c语言16进制数组转2进制数组的代码
时间: 2023-10-23 10:08:45 浏览: 194
MP3文件转C语言16进制数组工具
5星 · 资源好评率100%
以下是将16进制数组转换为2进制数组的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
void hexToBin(unsigned char *hexArray, int hexLength, unsigned char *binArray) {
int i, j;
for (i = 0; i < hexLength; i++) {
for (j = 0; j < 8; j++) {
binArray[i*8+j] = (hexArray[i] >> (7-j)) & 1;
}
}
}
int main() {
unsigned char hexArray[] = {0x1A, 0xB3, 0x2F};
int hexLength = sizeof(hexArray) / sizeof(hexArray[0]);
int binLength = hexLength * 8;
unsigned char *binArray = malloc(binLength * sizeof(unsigned char));
hexToBin(hexArray, hexLength, binArray);
printf("Hex Array: ");
for (int i = 0; i < hexLength; i++) {
printf("%02X ", hexArray[i]);
}
printf("\n");
printf("Bin Array: ");
for (int i = 0; i < binLength; i++) {
printf("%d", binArray[i]);
}
printf("\n");
free(binArray);
return 0;
}
```
这个程序会将16进制数组 `{0x1A, 0xB3, 0x2F}` 转换为二进制数组 `{00011010, 10110011, 00101111}`。
阅读全文