Felix Khazin
Felix Khazin

Reputation: 1487

How do you convert 8-bit bytes to 6-bit characters?

I have a specific requirement to convert a stream of bytes into a character encoding that happens to be 6-bits per character.

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 ".

What's the best way to do this programmatically (pseudo code or c++). Thank you!

Upvotes: 9

Views: 11110

Answers (2)

Alex Bakulin
Alex Bakulin

Reputation: 1668

Just in case if someone is interested - another variant that extracts 6-bit numbers from the stream as soon as they appear there. That is, results can be obtained even if less then 3 bytes are currently read. Would be useful for unpadded streams.

The code saves the state of the accumulator a in variable n which stores the number of bits left in accumulator from the previous read.

int n = 0;
unsigned char a = 0;
unsigned char b = 0;
while (read_byte(&byte)) {
    // save (6-n) most significant bits of input byte to proper position
    // in accumulator
    a |= (b >> (n + 2)) & (077 >> n);
    store_6bit(a);
    a = 0;
    // save remaining least significant bits of input byte to proper
    // position in accumulator
    a |= (b << (4 - n)) & ((077 << (4 - n)) & 077);
    if (n == 4) {
        store_6bit(a);
        a = 0;
    }
    n = (n + 2) % 6;
}

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1502086

Well, every 3 bytes, you end up with four characters. So for one thing, you need to work out what to do if the input isn't a multiple of three bytes. (Does it have padding of some kind, like base64?)

Then I'd probably take each 3 bytes in turn. In C#, which is close enough to pseudo-code for 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.
}

Upvotes: 7

Related Questions