输入:
char arr1[9] = "+100-200" // (+ is 2B, - is 2D, 1 is 31 and 2 is 32)
输出:
unsigned int arr2[4]= [0x2B31,0x3030,0x2D32,0x3030]
我该怎么做?
答案 0 :(得分:1)
您的问题似乎不一致:0
应转换为0x30
,即其ASCII值。
为什么要进行这种修改,代码非常简单:
char arr1[8] = "+100-200";
unsigned int arr2[4];
for (int i = 0; i < 8; i += 2) {
arr2[i / 2] = ((unsigned int)(unsigned char)arr1[i] << 8) |
(unsigned int)(unsigned char)arr1[i + 1];
}
for (int i = 0; i < 4; i++) {
printf("0x%04X ", arr2[i]);
}
printf("\n");
输出:
0x2B31 0x3030 0x2D32 0x3030