我在下面有这个数组:
dataIn[5] = 0x88;
dataIn[6] = 0x2A;
dataIn[7] = 0xC7;
dataIn[8] = 0x2B;
dataIn[9] = 0x00;
dataIn[10] = 0x28;
我需要将这些值转换为十进制,因为之后我需要将十进制值转换为 ASCII 并发送到 UART。
例如:
| Hexa | Decimal | ASCII (I need to send this data to UART)
| 0x882AC72B00 | 584 833 248 000 | 35 38 34 38 33 33 32 34 38 30 30 30
| 0x5769345612 | 375 427 192 338 | 33 37 35 34 32 37 31 39 32 33 33 38
我的问题:这些数据应该放在一起并转换为十进制,但我的编译器只有 4 个字节,我不知道该怎么做,因为我曾经有 5 个或更多字节。
PS.:我正在使用PIC18F46K80和C18编译器
[已编辑]
单击此处查看当我尝试使用超过 4 个字节时会发生什么。这是我的问题
有人可以帮助我吗?
提前谢谢。
如果我理解得很好,首先你应该定义一个这样的联合:
typedef union _DATA64
{
uint64_t dataIn64;
uint8_t dataIn8[8];
}tu_DATA64;
然后复制先前定义的联合中的十六进制值:
uint8_t i;
tu_DATA64 data;
...
data.dataIn64=0;
for(i=0; i<5; i++)
data.dataIn8[4-i]=dataIn[i];
现在你必须使用lltoa
函数转换字符串中的 64 位变量,如这篇文章中所建议的那样:
char *str;
...
str=lltoa(data.dataIn64,10);
str
是要发送的缓冲区字符串。
您是否考虑过编写自己的转换函数? 这是一个可以调整到任何长度的工作示例。
警告:我的C技能不是最好的!
#include <stdio.h>
/******************************************************************************/
void base10_ascii(unsigned char data[], int data_size, char ans[], int ans_size) {
char done;
do {
char r = 0;
done = 1;
for (int i=0; i<data_size; i++) {
int b = (r<<8) + data[i]; //previous remainder and current byte
data[i] = b / 10;
if (data[i] > 0) done = 0; //if any digit is non-zero, not done yet
r = b % 10;
}
for (int i=ans_size-1; i>0; i--) ans[i] = ans[i-1]; //bump up result
ans[0] = r + '0'; //save next digit as ASCII (right to left)
} while (!done);
}
/******************************************************************************/
int main(){
char outputBuffer[15] = {0};
char data[] = { 0x88, 0x2A, 0xC7, 0x2B, 0x00 }; //584833248000
base10_ascii(data,sizeof data,outputBuffer,sizeof outputBuffer);
printf("Output: %sn",outputBuffer);
return 0;
}