LISTSERV mailing list manager LISTSERV 16.5

Help for CSSA Archives


CSSA Archives

CSSA Archives


CSSA@LIST.UVM.EDU


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Monospaced Font

LISTSERV Archives

LISTSERV Archives

CSSA Home

CSSA Home

CSSA  February 2008

CSSA February 2008

Subject:

Interpreting between number bases that are expressed in powers of two

From:

Jacob Beauregard <[log in to unmask]>

Reply-To:

Computer Science Student Association <[log in to unmask]>

Date:

Tue, 12 Feb 2008 23:14:04 -0500

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (147 lines)

First order of business: A reminder that we are meeting tomorrow in
Votey 367 or whatever room into which we are forced.

Still, just for anyone that's interested:

I was writing an arbitrary map interpreter for games over winter break,
the environment in which tiles are placed in two dimensional games (like
Super Mario or the Legend of Zelda). I decided to store the coordinates
for the tiles on a map in a base64 format. I knew that I was going to
have to be able to edit maps, so I created functions to convert not only
from base 64 to native integers, but back to base64.

Recently Mike asked me a question regarding his project for HCI, in
which he intended to build a stack of the robots actions which he can
then reverse. I found this relevant to what I was doing with my
independent study in terms of the need to invert actions.

With Mike's project, I have a few things to state that may or may not be
issues with what he's envisioned.
    -Reversing physical actions is not always possible. The robot would
need to be designed such that every function can be reversed.
    -You cannot reverse an external force. The robot would only be
capable of reversing its own actions.
    -Inverting multi-threaded actions can lead to synchronization
problems, though it may be possible to minimize such problems.
    -It is not unusual for actions of the robot to be imprecise. This
will lead to a bit of error when inversing functions.

Anyway, I was going to talk about converting numbers that are in powers
of base 2. This isn't exactly related except for that it's focused on
inversion.

Converting to native integer (binary)
It is always easiest to keep in mind the number of digits you'll be
converting.

Octals (base 8): how many binary digits does it take to represent one
octal digit? Three.
    So if your interpreting an octal (in character representation)

char * oct = some_octal_number();
int i=0;
int result=0;
while (oct[i] != null) {
    result <<= 3;
    result |= oct[i++]-'0';
}

You can either add or bitwise-or the numerical value of the digit
(character). It needs to be kept in mind though, that -'0' is only a
shortcut, as that is the offset of all octal digits in their ASCII
representation.

In a base 16 or base 64 number system, there are more characters that
represent numbers

in base 16, the digits are typically 0123456789abcdef
in base 64, the digits are typically
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz and two
other symbols that are generally inconsistent across different
implementations.
In ASCII, 0-9 are all in sequence, A-Z are all in sequence, and a-z are
all in sequence.

So if you wanted to do base 16

char * hex = some_hex_number();
int i=0;
int result=0;
while (hex[i] != null) {
    result <<= 4;
    if (hex[i] >= '0' && hex[i] <= '9')
        result |= hex[i++]-'0';
    else
       result |= hex[i++]-'a';
}

We're assuming that the character array is a valid set of hexadecimal
digits.

Most programming languages provide a built-in means of translating
between binary, octal, decimal, and hexadecimal. This becomes more
useful if you need to represent things in larger number systems, such as
base32, base64, base128, and beyond. Of course, ASCII has only 255
characters, so I don't think anyone would be going much farther unless
you wanted to store huge numbers in files.

Mostly, the benefit of using larger number base systems is when you need
to store a lot of numerical information in a small amount of space. This
is typically useful for things such as coordinate data.

But if you're loading coordinates in from a file into native integer
data, how can you change that integer data back into larger number
bases? As I said, most programming languages offer this feature, but
only going as high as hexadecimal, so I will give a b64 example using
'+' as 63, and '/' as 64.

So you have to look at how many bits you're looking at at once.

Here's an actual function I wrote.

First you need to determine how many digits the character array will
require to represent the number in base 64 form. That can easily be
achieved by a bitwise right shift. I use this number not only to
determine how much memory has to be allocated, but as a means to
determine the position in the array where I need to insert a base64
digit, and how many digits are left to enter into the array.

After this, you will need to start loading into the character array 6
bits of the number at a time (representative of a single digit). This
can be achieved by isolating the last 6 bits using a bitwise-and. Once
those 6 bits are isolated and stored as a representative digit in the
array, you set the corresponding position in the array to the ASCII
character representing those six bits.

Since I don't care what those six bits are anymore, I right bit-shift
them out of the integer that I'm converting to base 64.

char * b64_encode(unsigned int num) {
    unsigned int working_num;
    unsigned int num_digits = num >> 6;
    char * result = new char[num_digits+1];
    result[num_digits] = '\0';
    while (num != 0) {
        num_digits--;
        working_num = num & 63;
        num >>= 6;
        if (working_num >= (int) ('A' - 55) && working_num <= (int) ('Z'
- 55))
            result[num_digits] = (char) working_num + 55;
        else if (working_num >= (int) ('a' - 60) && working_num <= (int)
('z' - 60))
            result[num_digits] = (char) working_num + 60;
        else if (working_num >= 0 && working_num <= 9)
            result[num_digits] = (char) working_num + '0';
        else if (working_num == 62)
            result[num_digits] = '+';
        else if (working_num == 63)
            result[num_digits] = '/';
    }
    return result;
}

Anyways, I hope to be able to make the meeting tomorrow, but that
largely depends on how much studying I get done tonight for my exams
tomorrow and Thursday.

Top of Message | Previous Page | Permalink

Advanced Options


Options

Log In

Log In

Get Password

Get Password


Search Archives

Search Archives


Subscribe or Unsubscribe

Subscribe or Unsubscribe


Archives

November 2020
July 2020
April 2020
March 2020
February 2020
July 2019
March 2019
September 2018
June 2018
August 2017
July 2017
June 2017
October 2016
September 2016
August 2016
July 2016
June 2016
April 2016
October 2012
August 2012
May 2012
April 2012
March 2012
February 2012
January 2012
December 2011
November 2011
October 2011
September 2011
August 2011
July 2011
June 2011
May 2011
April 2011
March 2011
February 2011
January 2011
December 2010
November 2010
October 2010
September 2010
August 2010
July 2010
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
December 2009
November 2009
October 2009
September 2009
August 2009
July 2009
May 2009
April 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
June 2008
May 2008
April 2008
March 2008
February 2008
January 2008
December 2007
November 2007
October 2007
September 2007
August 2007
May 2007
April 2007
March 2007
February 2007
January 2007
November 2006
October 2006
September 2006
June 2006
May 2006
April 2006
March 2006
January 2006
December 2005
November 2005
October 2005
September 2005
July 2005
May 2005
April 2005
March 2005
February 2005
January 2005
November 2004
October 2004
September 2004
August 2004
July 2004
June 2004
May 2004
April 2004
March 2004
February 2004
January 2004
December 2003
November 2003
October 2003
September 2003
August 2003
July 2003
June 2003
May 2003
April 2003
March 2003
February 2003
January 2003
December 2002
November 2002
October 2002
September 2002
August 2002
May 2002
April 2002
February 2002
January 2002
December 2001
November 2001
October 2001
September 2001
July 2001
May 2001
April 2001
March 2001
February 2001
January 2001
December 2000
November 2000
October 2000
September 2000
August 2000
June 2000
May 2000
April 2000
February 2000
January 2000
November 1999
October 1999
September 1999
July 1999
April 1999
March 1999
January 1999
November 1998
October 1998
September 1998
August 1998
July 1998
April 1998
March 1998

ATOM RSS1 RSS2



LIST.UVM.EDU

CataList Email List Search Powered by the LISTSERV Email List Manager