java base64 to hex,变换Base64的字符串作为BASE16(HEX)字符串?

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

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值