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 multithreaded 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 bitwiseor 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, 09 are all in sequence, AZ are all in sequence, and az 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 builtin 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 bitwiseand. 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 bitshift
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.
