如何将8位字节转换为6位字符?

Fel*_*zin 7 character-encoding

我有一个特定的要求,即将字节流转换为每个字符恰好为6位的字符编码.

Here's an example:

Input: 0x50 0x11 0xa0

Character Table:

010100 T
000001 A
000110 F
100000 SPACE


Output: "TAF "

Logically I can understand how this works:

Taking 0x50 0x11 0xa0 and showing as binary:

01010000 00010001 10100000

Which is "TAF ".
Run Code Online (Sandbox Code Playgroud)

以编程方式执行此操作的最佳方法是什么(伪代码或c ++).谢谢!

Jon*_*eet 6

好吧,每3个字节,你最终会得到4个字符.所以首先,如果输入不是三个字节的倍数,你需要弄清楚要做什么.(它是否有某种填充,如base64?)

然后我可能依次取每个3个字节.在C#中,它足够接近C代码的伪代码:)

for (int i = 0; i < array.Length; i += 3)
{
    // Top 6 bits of byte i
    int value1 = array[i] >> 2;
    // Bottom 2 bits of byte i, top 4 bits of byte i+1
    int value2 = ((array[i] & 0x3) << 4) | (array[i + 1] >> 4);
    // Bottom 4 bits of byte i+1, top 2 bits of byte i+2
    int value3 = ((array[i + 1] & 0xf) << 2) | (array[i + 2] >> 6);
    // Bottom 6 bits of byte i+2
    int value4 = array[i + 2] & 0x3f;

    // Now use value1...value4, e.g. putting them into a char array.
    // You'll need to decode from the 6-bit number (0-63) to the character.
}
Run Code Online (Sandbox Code Playgroud)