开发者

Transform BASE64 string to BASE16(HEX) string?

开发者 https://www.devze.com 2023-02-21 16:00 出处:网络
Hey, I\'m trying to write a program to convert from a BASE64 string to a BASE16(HEX) string. Here\'s an example:

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 :)


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


think of base-64 as base(2^6) so in order to get alignment with a hex nibble you need at least 2 base 64 digits...

with 2 base-64 digits you have a base-(2^12) number, which could be represented by 3 2^4 digits...

(00)(01)(02)(03)(04)(05)---(06)(07)(08)(09)(10)(11) (base-64) maps directly to:

(00)(01)(02)(03)---(04)(05)(06)(07)---(08)(09)(10)(11) (base 16)

so you can either convert to contiguous binary... or use 4 operations... the operations could deal with binary values, or they could use a set of look up tables (which could work on the char encoded digits):

  1. first base-64 digit to first hex digit
  2. first base-64 digit to first half of second hex digit
  3. second base-64 digit to second half of second hex digit
  4. second base-64 digit to third hex digit.

the advantage of this is that you can work on encoded bases without binary conversion. it is pretty easy to do in a stream of chars... I am not aware of this actually being implemented anywhere though.

0

精彩评论

暂无评论...
验证码 换一张
取 消