Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform BASE64 string to BASE16(HEX) string?

Tags:

c

Hey, I'm trying to write a program to convert from a BASE64 string to a BASE16(HEX) string.

Here's an example:

     BASE64:   Ba7+Kj3N
HEXADECIMAL:   05 ae fe 2a 3d cd
     BINARY:   00000101 10101110 11111110 00101010 00111101 11001101
    DECIMAL:   5 174 254 42 61 205

What's the logic to convert from BASE64 to HEXIDECIMAL?
Why is the decimal representation split up?
How come the binary representation is split into 6 section?

Just want the math, the code I can handle just this process is confusing me. Thanks :)

like image 401
eveo Avatar asked Apr 03 '11 22:04

eveo


People also ask

Is Hex same as Base64?

The difference between Base64 and hex is really just how bytes are represented. Hex is another way of saying "Base16". Hex will take two characters for each byte - Base64 takes 4 characters for every 3 bytes, so it's more efficient than hex.

How do I decode a Base64 string?

To decode with base64 you need to use the --decode flag. With encoded string, you can pipe an echo command into base64 as you did to encode it. Using the example encoding shown above, let's decode it back into its original form. Provided your encoding was not corrupted the output should be your original string.

Does Base64 end in ==?

The equals sign "=" represents a padding, usually seen at the end of a Base64 encoded sequence. The size in bytes is divisible by three (bits divisible by 24): All bits are encoded normally.


1 Answers

Here's a function listing that will convert between any two bases: https://sites.google.com/site/computersciencesourcecode/conversion-algorithms/base-to-base


Edit (Hopefully to make this completely clear...)

You can find more information on this at the Wikipedia entry for Base 64.

The customary character set used for base 64, which is different than the character set you'll find in the link I provided prior to the edit, is:

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/

The character 'A' is the value 0, 'B' is the value 1, 'C' is the value 2, ...'8' is the value 60, '9' is the value 61, '+' is the value 62, and '/' is the value 63. This character set is very different from what we're used to using for binary, octal, base 10, and hexadecimal, where the first character is '0', which represents the value 0, etc.

Soju noted in the comments to this answer that each base 64 digit requires 6 bits to represent it in binary. Using the base 64 number provided in the original question and converting from base 64 to binary we get:

B        a        7        +        K        j        3        N
000001   011010   111011   111110   001010   100011   110111   001101

Now we can push all the bits together (the spaces are only there to help humans read the number):

000001011010111011111110001010100011110111001101

Next, we can introduce new white-space delimiters every four bits starting with the Least Significant Bit:

0000  0101  1010  1110  1111  1110  0010  1010  0011  1101  1100  1101

It should now be very easy to see how this number is converted to base 16:

0000  0101  1010  1110  1111  1110  0010  1010  0011  1101  1100  1101
0     5     A     E     F     E     2     A     3     D     C     D
like image 110
oosterwal Avatar answered Oct 15 '22 18:10

oosterwal