great! thanks, do you have sample ANSI C code (even just the logic part)
Main Topics
Browse All TopicsI have a to decode a data to base64 string. I have the hex value of each character of a base64 string.
my limited knowledge is through this:
http://en.wikipedia.org/wi
where you can convert a hex to decimal and then to base64 string base on the logic.
are there any simple and efficient ANSI C code available (without linking to other base64.h codes)
thanks!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
The wiki you mentioned contains a link to this ANSI C implementation :
http://base64.sourceforge.
Would that suit your needs ? If not, why not ?
I suddenly had to think back to this question :
http://www.experts-exchang
That was a fun challenge :)
The following data strings will simplify your algorithm:
BIN = 00000001001000110100010101
HEX = 0123456789ABCDEF
B64 = ABCDEFGHIJKLMNOPQRSTUVWXYZ
Use the BIN string to initialise an array of nybbles which will be indexed by their equivalent HEX value as in the following:
h[0] = 0000
h[1] = 0001
:
h[9] = 1001
h[A] = 1010
:
h[F] = 1111
So the array h[ ] will be indexed by the enumerated types 0~9, A~F.
Therefore, if you were to input the value '5FA' in hex, then a loop could take each character in turn using something similar to the MID$() function and produce the following results:
h[5] = 0101
h[F] = 1111
h[A] = 1010
During each iteration of the loop, you could append these nybbles to a string as in the following:
binary$ = h[5] + h[F] + h[A]
This would produce the following:
binary$ = 010111111010
The code for this might look something like the following:
LET hex$ = 5FA
FOR i = 0 TO LEN(hex$ - 1) STEP 1
LET binary$ = binary$ + h[MID$(hex$, i, 1))]
NEXT i
The next process involves two stages: converting 6-bit parts to decimal and then using this decimal value to index the B64 string above to return the equivalent base-64 character.
Again, using something similar to the MID$ function, loop through the binary$ string in steps of 6 thereby selecting 6 binary bits at a time and convert them their decimal value. The following will step through the binary$ string:
FOR i = 0 TO LEN(binary$ - 1) STEP 6
LET 6bits = MID$(binary$, i, 6)
:
NEXT i
Before doing this, it may be necessary to pad the binary$ string with leading zeros so that it's length is a multiple of 6. Doing so, you would then need to check whether the first 6 bits are all zero and if so, chop them off.
Inside the FOR loop, you would need to convert the 6-bit binary value to decimal as in the following:
LET decimal = 0
LET multiplyer = 1
FOR i = 5 TO 0 STEP -1
IF MID$(6bit$, i, 1) = 1 THEN LET decimal = decimal + multiplyer
LET muliplyer = multiplyer x 2
NEXT i
At this stage, you will have a decimal value which can now be used to index the B64 string above, as in:
LET base64$ = B64[decimal]
For each iteration of the first FOR loop, the value returned by B64[decimal] is appended to the base64$ string using the following:
LET base64$ = base64$ = B64[decimal]
And there you have it.
I have used a pseudo-code approach in my explanation which I hope makes sense to you.
>> Before doing this, it may be necessary to pad the binary$ string with leading zeros so that it's length is a multiple of 6.
A Base64 encoding uses '=' characters at the end for padding it to a multiple of 3 characters. You don't have to add any further padding ... Just process the data 6 bits at a time (or multiples of 6 bits, depending on your approach).
An "easy" approach is to process the Base64 data 4 characters (24 bits) at a time, which corresponds to 3 bytes of binary data. And vice versa.
There are lookup table approaches of all kinds, platform specific optimizations, etc.
Base64 can be explained quite simply like this :
encoding : Take binary data, and split it up in blocks of 6 bits. Each such block is mapped to a character in the Base64 alphabet (A-Z, a-z, 0-9, '+' and '/'), which is pushed on the output stream.
decoding : Take each character, and transform it to the original block of 6 bits it corresponds to by using the Base64 alphabet, and push those 6 bits on the output stream.
That's it.
Infinty08
I thought this was an academic exercise in developing an algorithm so I re-read the question and realise the asker wants C code and, quite possibly, develop a program to convert a stream (file-to-file, say) from hex to base64.
Under those circumstances, I agree it is easier to decode 3 octets at a time and that padding with '=' is the usual approach.
For speed, I would define two arrays: h[...], as described in my previous post:
h[0] = 0000
h[1] = 0001
:
h[9] = 1001
h[A] = 1010
:
h[F] = 1111
indexed by enumerated types 0~9, A~F and, another b64[...]:
b64[000000] = A
b64[000001] = B
b64[000010] = C
:
b64[111101] = 9
b64[111110] = +
b64[111111] = /
indexed by enurated sextets.
Then, simply read in 3 hex digits at a time. Convert them to their corresponding nibbles like this:
(As an example, our stream starts like this: 2FA50F6... therefore,
digits read in = '2', 'F' and 'A'
these are read into a variable ch, so,
ch = '2' therefore, h[ch] = '0010' --> append this to our binary string. Binary = 0010
ch = 'F' therefore, h[ch] = '1111' --> append this to our binary string. Binary = 00101111
ch = 'A' therefore, h[ch] = '1010' --> append this to our binary string. Binary = 001011111010
we now arrive at a 2-sextet wide binary string which we can split into 2 separate sextet enumerate patterns which are used to index the b64[...] array as in the following:
sextet = '001011' therefore, b64[sextet] = 'K' --> append to base64 string. Base64 = K
sextet = '111010' therefore, b64[sextet] = '6' --> append to base64 string. Base64 = K6
and continue this process for all the hex digits padding with '=' at the end if necessary.
So, rather than actually perform any arithmetical calculations, simply use the values obtained as indicies to obtain the translated value from hex, to sextets, to base64.
Business Accounts
Answer for Membership
by: thehagmanPosted on 2009-08-05 at 13:33:26ID: 25027824
Each character in base64 encodes 6 bits, each character in hex encodes 4 bits.
Therefore it is more efficient to avoid any decimal intermediate.
Three hex digits produce 2 base64 cracters.
If these three hex digits correspond to numerical values x,y,z, then
the first base64 character corresponds to (x<<2) + (y>>2) and the second to ((y & 3)<< 4) + z.