我正在尝试以特定形式将整数或浮点数转换为 char 数组(为 mbed 编程,一个微控制器)。
整数应该是 32 位大端二进制补码整数浮点数应该是 32 位大端 IEEE 754 浮点数
尝试了几件事:
uint8_t *v;
uint8_t valuePos = 3;
v = (uint8_t *)datum->data.i; //I get the int or float from datum->data.i
buff[lengthEnd++] = v[valuePos--]; //buff is the char array
buff[lengthEnd++] = v[valuePos--];
buff[lengthEnd++] = v[valuePos--];
buff[lengthEnd++] = v[valuePos];
和
uint32_t i = BigEndian(datum->data.i);
uint8_t * ptr = (uint8_t *) &i;
strcat(buff, (char *) ptr); //maybe strcat isn't a good function to use here
lengthEnd += 4;
但我不能让它工作,我总是得到一些其他的数字。出了什么问题?