问题描述
我目前正在研究一个hex-> base64转换器。我如何支持处理奇数个十六进制数字?我到现在为止所做的是每个十六进制数字都是一个4位数字,所以2个十六进制数字是1个字节。如果我遇到奇数个十六进制数字,我只需用0填充未完成字节的剩余部分?或者我应该返回一个错误?
I'm currently working on an hex->base64 converter. How am I suppsoed to handle an odd number of hex-digits? What I did until now is that every hex-digit is an 4-bit number, so 2 hex-digits are 1 byte. If I encounter an odd number of hex-digits do I simply fill the rest of the uncomplete byte with 0? Or am I supposed to return an error?
推荐答案
发现这个问题工作在cryptopals挑战自己。答案是,根据
Found this question working on cryptopals challenge myself. The answer is, according to Wikipedia:
- 添加填充符,以便生成的字符串可以用3个字节整除。
- 将填充的4位值设置为0(取决于您以前做过或在hexstring转换之后,这是一个实际的0或'0')
- 在结果的base64字符串中,填充的4位值的数量在end
0x41414141的实现似乎不是一个完整的解决方案,因为从hexstring到binary的转换是手动硬编码的。此外,我不明白为什么应该删除前导零。
The implementation of 0x41414141 seems not be a complete solution for the challenge, as the conversion from hexstring to binary is manually hardcoded. Besides, I don't understand why leading zeros should be removed.
这篇关于十六进制转换为base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!