c语言怎样byte[]转Hex
时间: 2023-12-29 07:04:55 浏览: 166
byte数组转化为hex
在C语言中,可以使用以下代码将byte数组转换为Hex:
```c
#include <stdio.h>
void byteToHex(unsigned char *byteArray, int byteArrayLen, char *hexArray) {
char hexDigits[] = "0123456789ABCDEF";
int i;
for (i = 0; i < byteArrayLen; i++) {
hexArray[i*2] = hexDigits[byteArray[i] >> 4];
hexArray[i*2+1] = hexDigits[byteArray[i] & 0x0F];
}
hexArray[byteArrayLen*2] = '\0';
}
int main() {
unsigned char byteArray[] = {0x12, 0x34, 0xAB, 0xCD};
char hexArray[sizeof(byteArray)*2+1];
byteToHex(byteArray, sizeof(byteArray), hexArray);
printf("%s\n", hexArray);
return 0;
}
```
输出结果为:1234ABCD
阅读全文