开发者

How to turn this simple 5 bytes back into 4 bytes ? (algorithm to convert 4 bytes into 5 bytes is known)

The algorithm to convert input 8 digit hex number into 10 digit are following:

Given that the 8 digit number is: '12 34 56 78'

x1 = 1 * 16^8 * 2^3
x2 = 2 * 16^7 * 2^2
x3 = 3 * 16^6 * 2^1
x4 = 4 * 开发者_如何转开发16^4 * 2^4
x5 = 5 * 16^3 * 2^3
x6 = 6 * 16^2 * 2^2
x7 = 7 * 16^1 * 2^1
x8 = 8 * 16^0 * 2^0

Final 10 digit hex is:
=> x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 
=> '08 86 42 98 E8'

The problem is - how to go back to 8 digit hex from a given 10 digit hex (for example: 08 86 42 98 E8 to 12 34 56 78)

Some sample input and output are following:

input                output

11 11 11 11          08 42 10 84 21
22 22 33 33          10 84 21 8C 63
AB CD 12 34          52 D8 D0 88 64
45 78 96 32          21 4E 84 98 62
FF FF FF FF          7B DE F7 BD EF

P.S. The problem I think is not limited to 8 or 10 digits. If the input is 11, the output would be 08.


From this conversion algorithm:

x1 = 1 * 16^8 * 2^3
x2 = 2 * 16^7 * 2^2
x3 = 3 * 16^6 * 2^1
x4 = 4 * 16^4 * 2^4
x5 = 5 * 16^3 * 2^3
x6 = 6 * 16^2 * 2^2
x7 = 7 * 16^1 * 2^1
x8 = 8 * 16^0 * 2^0

You can see that after 16^4 it skipped to 16^6. Pull one off so that it arranges nicely.

x1 = 1 * 16^7 * 16^1 * 2^3
x2 = 2 * 16^6 * 16^1 * 2^2
x3 = 3 * 16^5 * 16^1 * 2^1
x4 = 4 * 16^4 * 2^4
x5 = 5 * 16^3 * 2^3
x6 = 6 * 16^2 * 2^2
x7 = 7 * 16^1 * 2^1
x8 = 8 * 16^0 * 2^0

16^1 is 2^4, so

x1 = 1 * 16^7 * 2^4 * 2^3
x2 = 2 * 16^6 * 2^4 * 2^2
x3 = 3 * 16^5 * 2^4 * 2^1
x4 = 4 * 16^4 * 2^4
x5 = 5 * 16^3 * 2^3
x6 = 6 * 16^2 * 2^2
x7 = 7 * 16^1 * 2^1
x8 = 8 * 16^0 * 2^0

Putting them together, you will see that the power goes up nicely.

x1 = 1 * 16^7 * 2^7
x2 = 2 * 16^6 * 2^6
x3 = 3 * 16^5 * 2^5
x4 = 4 * 16^4 * 2^4
x5 = 5 * 16^3 * 2^3
x6 = 6 * 16^2 * 2^2
x7 = 7 * 16^1 * 2^1
x8 = 8 * 16^0 * 2^0

And multiplying with 2^something can be seen as a shift left operator.

x1 = 1 * 16^7 << 7
x2 = 2 * 16^6 << 6
x3 = 3 * 16^5 << 5
x4 = 4 * 16^4 << 4
x5 = 5 * 16^3 << 3
x6 = 6 * 16^2 << 2
x7 = 7 * 16^1 << 1
x8 = 8 * 16^0 << 0

16^something is for multiplying with base 16. So that, these 4 bytes of number

AAAABBBB CCCCDDDD EEEEFFFF GGGGHHHH

Become 5 bytes of this:

0AAAA0BB BB0CCCC0 DDDD0EEE E0FFFF0G GGG0HHHH

So using that picture, you can create a function that takes the 10 digit hex number and outputs into the 4 digit hex numbers using simple bitwise operations.

For simplicity, I will use unsigned char on this example C code:

void convert(unsigned char five[], unsigned char four[]) {
    four[0] = (five[0] << 1) & 0xF0  // 11110000
            | (five[0] << 2) & 0x0C  // 00001100
            | (five[1] >> 6) & 0x03; // 00000011
    four[1] = (five[1] << 3) & 0xF0  // 11110000
            | (five[2] >> 4) & 0x0F; // 00001111
    four[2] = (five[2] << 5) & 0xE0  // 11100000
            | (five[3] >> 3) & 0x10  // 00010000
            | (five[3] >> 2) & 0x0F; // 00001111
    four[3] = (five[3] << 7) & 0x80  // 10000000
            | (five[4] >> 1) & 0x70  // 01110000
            | (five[4])      & 0x0F; // 00001111
}

And the output (see the full code):

 08 42 10 84 21      11 11 11 11
 10 84 21 8C 63      22 22 33 33
 52 D8 D0 88 64      AB CD 12 34
 21 4E 84 98 62      45 78 96 32
 7B DE F7 BD EF      FF FF FF FF
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜