试图在c中将十六进制格式化为小端
trying to format hex into little endian in c
所以我正在尝试获取缓冲区中值的总和,但是,需要重新排列这些值才能这样做。本质上,我如何让 C 将此数组中的值转换为:uint16_t inp[] = {0xFF,0x81,0xFD,0x00,0x00,0x00,0x00,0x08,0x00,0x00};
看起来像此数组:uint16_t buff[] = {0x81FF,0x00FD,0x0000,0x0800,0x0000}
感谢@kaylum,我明白了:
#include <stdio.h>
#include <stdint.h>
uint16_t inp[] = {0xFF,0x81,0xFD,0x00,0x00,0x00,0x00,0x08,0x00,0x00};
uint16_t buff[sizeof(inp)/2];
int main(){
int sum = 0;
for(int i = 0; i < sizeof(buff);i=i+2){
buff[i] = ((inp[i+1] | 0x00) << 8) | (inp[i] | 0x00);
//printf("%.4X\n",buff[i]);
}
for(int i = 0; i < sizeof(buff);i++){
sum = sum += buff[i];
}
printf("%u\n",sum);
return 0;
}
所以我正在尝试获取缓冲区中值的总和,但是,需要重新排列这些值才能这样做。本质上,我如何让 C 将此数组中的值转换为:uint16_t inp[] = {0xFF,0x81,0xFD,0x00,0x00,0x00,0x00,0x08,0x00,0x00};
看起来像此数组:uint16_t buff[] = {0x81FF,0x00FD,0x0000,0x0800,0x0000}
感谢@kaylum,我明白了:
#include <stdio.h>
#include <stdint.h>
uint16_t inp[] = {0xFF,0x81,0xFD,0x00,0x00,0x00,0x00,0x08,0x00,0x00};
uint16_t buff[sizeof(inp)/2];
int main(){
int sum = 0;
for(int i = 0; i < sizeof(buff);i=i+2){
buff[i] = ((inp[i+1] | 0x00) << 8) | (inp[i] | 0x00);
//printf("%.4X\n",buff[i]);
}
for(int i = 0; i < sizeof(buff);i++){
sum = sum += buff[i];
}
printf("%u\n",sum);
return 0;
}