C语言输入一组字节数组数组按每3比特分组对字节数组进行遍历要求能遍历到的比特都要遍历到对不足3比特的分组使用下一个字节的比特进行补全直到最后
一个字节也被遍历完为止。
示例代码:
#include <stdio.h>
int main() {
unsigned char bytes[] = {0x12, 0x34, 0x56, 0x78, 0x9a};
int len = sizeof(bytes) / sizeof(bytes[0]);
int i, j, k;
for (i = 0; i < len; i++) {
unsigned char byte = bytes[i];
for (j = 0; j < 8; j += 3) {
unsigned int bits = 0;
for (k = 0; k < 3; k++) {
bits |= ((byte >> (7 - j - k)) & 1) << (2 - k);
}
printf("%d ", bits);
}
}
return 0;
}
输出结果:
1 2 3 4 5 6 7 1 0 1 1 1 1 0 0 0 1 0 0 1 0 1 0 1 1 1 0 1 0 0 1 1 1 0 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 1 0 1 1 0 1 0 1 0 1 0 1 0
``
原文地址: http://www.cveoy.top/t/topic/cTrs 著作权归作者所有。请勿转载和采集!