繁体   English   中英

连接5个或更多字节,然后转换为十进制,然后转换为ASCII

[英]Concatenate 5 or more bytes and convert to decimal and then to ASCII

我在下面有这个数组:

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个或更多字节。

附:我正在使用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];

现在你有使用到64位变量转换的字符串lltoa功能,如建议在这个职位

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: %s\n",outputBuffer);
  return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM