我遇到需要将动态大小的字节数组升级3的情况。
示例:
10101010 11001100
到
11100011 10001110 00111000 11111100 00001111 11000000
我在这里使用算法来生成查找表。
https://stackoverflow.com/a/9044057/280980
static const uint32_t bitExpandTable[256] = {
00000000, 0x000007, 0x000038, 0x00003f, 0x0001c0, 0x0001c7, 0x0001f8, 0x0001ff,
0x000e00, 0x000e07, 0x000e38, 0x000e3f, 0x000fc0, 0x000fc7, 0x000ff8, 0x000fff,
0x007000, 0x007007, 0x007038, 0x00703f, 0x0071c0, 0x0071c7, 0x0071f8, 0x0071ff,
0x007e00, 0x007e07, 0x007e38, 0x007e3f, 0x007fc0, 0x007fc7, 0x007ff8, 0x007fff,
0x038000, 0x038007, 0x038038, 0x03803f, 0x0381c0, 0x0381c7, 0x0381f8, 0x0381ff,
0x038e00, 0x038e07, 0x038e38, 0x038e3f, 0x038fc0, 0x038fc7, 0x038ff8, 0x038fff,
0x03f000, 0x03f007, 0x03f038, 0x03f03f, 0x03f1c0, 0x03f1c7, 0x03f1f8, 0x03f1ff,
0x03fe00, 0x03fe07, 0x03fe38, 0x03fe3f, 0x03ffc0, 0x03ffc7, 0x03fff8, 0x03ffff,
0x1c0000, 0x1c0007, 0x1c0038, 0x1c003f, 0x1c01c0, 0x1c01c7, 0x1c01f8, 0x1c01ff,
0x1c0e00, 0x1c0e07, 0x1c0e38, 0x1c0e3f, 0x1c0fc0, 0x1c0fc7, 0x1c0ff8, 0x1c0fff,
0x1c7000, 0x1c7007, 0x1c7038, 0x1c703f, 0x1c71c0, 0x1c71c7, 0x1c71f8, 0x1c71ff,
0x1c7e00, 0x1c7e07, 0x1c7e38, 0x1c7e3f, 0x1c7fc0, 0x1c7fc7, 0x1c7ff8, 0x1c7fff,
0x1f8000, 0x1f8007, 0x1f8038, 0x1f803f, 0x1f81c0, 0x1f81c7, 0x1f81f8, 0x1f81ff,
0x1f8e00, 0x1f8e07, 0x1f8e38, 0x1f8e3f, 0x1f8fc0, 0x1f8fc7, 0x1f8ff8, 0x1f8fff,
0x1ff000, 0x1ff007, 0x1ff038, 0x1ff03f, 0x1ff1c0, 0x1ff1c7, 0x1ff1f8, 0x1ff1ff,
0x1ffe00, 0x1ffe07, 0x1ffe38, 0x1ffe3f, 0x1fffc0, 0x1fffc7, 0x1ffff8, 0x1fffff,
0xe00000, 0xe00007, 0xe00038, 0xe0003f, 0xe001c0, 0xe001c7, 0xe001f8, 0xe001ff,
0xe00e00, 0xe00e07, 0xe00e38, 0xe00e3f, 0xe00fc0, 0xe00fc7, 0xe00ff8, 0xe00fff,
0xe07000, 0xe07007, 0xe07038, 0xe0703f, 0xe071c0, 0xe071c7, 0xe071f8, 0xe071ff,
0xe07e00, 0xe07e07, 0xe07e38, 0xe07e3f, 0xe07fc0, 0xe07fc7, 0xe07ff8, 0xe07fff,
0xe38000, 0xe38007, 0xe38038, 0xe3803f, 0xe381c0, 0xe381c7, 0xe381f8, 0xe381ff,
0xe38e00, 0xe38e07, 0xe38e38, 0xe38e3f, 0xe38fc0, 0xe38fc7, 0xe38ff8, 0xe38fff,
0xe3f000, 0xe3f007, 0xe3f038, 0xe3f03f, 0xe3f1c0, 0xe3f1c7, 0xe3f1f8, 0xe3f1ff,
0xe3fe00, 0xe3fe07, 0xe3fe38, 0xe3fe3f, 0xe3ffc0, 0xe3ffc7, 0xe3fff8, 0xe3ffff,
0xfc0000, 0xfc0007, 0xfc0038, 0xfc003f, 0xfc01c0, 0xfc01c7, 0xfc01f8, 0xfc01ff,
0xfc0e00, 0xfc0e07, 0xfc0e38, 0xfc0e3f, 0xfc0fc0, 0xfc0fc7, 0xfc0ff8, 0xfc0fff,
0xfc7000, 0xfc7007, 0xfc7038, 0xfc703f, 0xfc71c0, 0xfc71c7, 0xfc71f8, 0xfc71ff,
0xfc7e00, 0xfc7e07, 0xfc7e38, 0xfc7e3f, 0xfc7fc0, 0xfc7fc7, 0xfc7ff8, 0xfc7fff,
0xff8000, 0xff8007, 0xff8038, 0xff803f, 0xff81c0, 0xff81c7, 0xff81f8, 0xff81ff,
0xff8e00, 0xff8e07, 0xff8e38, 0xff8e3f, 0xff8fc0, 0xff8fc7, 0xff8ff8, 0xff8fff,
0xfff000, 0xfff007, 0xfff038, 0xfff03f, 0xfff1c0, 0xfff1c7, 0xfff1f8, 0xfff1ff,
0xfffe00, 0xfffe07, 0xfffe38, 0xfffe3f, 0xffffc0, 0xffffc7, 0xfffff8, 0xffffff,
};
我尝试使用LUT循环遍历字节数组,以便将前3个字节存储到新数组中。但是我的输出看起来不正确。
有没有人对如何有效实施这个有任何建议?这将在嵌入式ARM处理器上运行。
LUT测试
uint8_t msg[] = { 0xaa, 0x02, 0x43, 0x5a, 0x8d, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd0, 0x84, 0xc6, 0x2d, 0x00, 0xb9 };
uint8_t expanded_msg[63] = { 0 };
uint8_t tmp_val = 0;
uint32_t lut_val;
for (int i = 0; i < 21; i++)
{
tmp_val = *(uint8_t*)(&msg + i);
lut_val = bitExpandTable[tmp_val];
memcpy(&expanded_msg[(i * 3)], &lut_val, 3);
}
print_binary(&msg, 21);
print_binary(&expanded_msg, sizeof(expanded_msg));
输出
[ 10101010 00000010 01000011 01011010 10001101 00000110 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 11010000 10000100 11000110 00101101 00000000 10111001 ]
[ 00111000 10001110 11100011 00000000 00000000 00000000 11000111 10001111 00000011 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000111 01110000 00011100 00000111 11110000 00000011 11111111 11111111 00011111 11000111 11110001 11100011 00000000 00000000 00000000 11111111 11111111 11111111 11000000 10000001 11100011 00000000 00000000 00000000 00000111 01110000 00011100 00111000 10000000 00011111 11111111 11111111 00011111 11000111 11110001 11100011 00000000 00000000 00000000 11111111 11111111 11111111 ]
答案 0 :(得分:4)
您的表格存在字节顺序问题(或您复制数据的方式):
对于第一个输入字节
10101010
您的输出是
a b c
00111000 10001110 11100011
但应该是
c b a
11100011 10001110 00111000
因此需要交换第1个和第3个字节(依此类推)
我没有改变表格以适应memcpy,而是做一些像
这样的事情int j = 0;
for (int i = 0; i < 21; i++) {
lut_val = bitExpandTable[msg[i]];
expanded_msg[j++] = (uint8_t) (lut_val >> 16);
expanded_msg[j++] = (uint8_t) (lut_val >> 8);
expanded_msg[j++] = (uint8_t) lut_val;
}