【问题标题】:Concatenate 5 or more bytes and convert to decimal and then to ASCII连接 5 个或更多字节并转换为十进制,然后转换为 ASCII
【发布时间】:2018-01-28 18:10:52
【问题描述】:

我在下面有这个数组:

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 编译器

[已编辑]

Click here to see what happen when I try to use more than 4 bytes. This is my problem

谁能帮帮我?

提前致谢。

【问题讨论】:

  • 如何对“十进制”值进行分组?每个十进制值使用多少位?你如何将五个字节分成四个十进制值?抱歉重复了,同样的问题问了三次,但这对于帮助理解您的问题以及如何解决问题非常重要。
  • 为什么需要将它们作为字符发送?用于展示?
  • 那些数据来自UART,所以我一一获取,放入这个数组中。 “我如何对十进制值进行分组?”,我使用按位移位,但这是我的问题,我不能这样做,因为我的编译器最多可以使用 4 个字节。
  • 是的,它是用来显示数据的,但我会将这些数据发送到我的 Android 应用程序,这个应用程序能够“理解”只是 ASCII。

标签: c microcontroller microchip c18


【解决方案1】:

如果我理解的很好,首先你应该像这样定义一个联合:

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 位变量转换为字符串,就像在此 post 中建议的那样:

char *str;

...

str=lltoa(data.dataIn64,10);

str 是要发送的缓冲区字符串。

【讨论】:

  • 我的编译器没有“long long”。我更大的变量有 4 个字节(长),这是我的问题。当我尝试设置一个 8 个字节的变量并打开调试检查时,我只能看到 4 个字节,因为这是我的编译器的最大值。在这种情况下,我该怎么办?我应该像你之前提到的那样创建工会吗?
  • 我在上面的问题中附上了一张图片,并附有示例。也许它可以帮助您更好地了解我需要什么。同样的方法,谢谢你的回复
  • 但是您是否尝试包含stdint.h 库并使用uint64_t 声明变量long long?通常使用stdint.h
  • 是的,我做到了,但我拥有的最大变量是 uint32_t,xc8 编译器不支持 uint64_t。这就是为什么我不知道如何处理它
  • 抱歉耽搁了。我认为唯一的解决方案是实现一个自定义itoa 函数,将字节缓冲区转换为字符串。您可以从这个网站获得提示:link。重点是在缓冲区而不是 int 上重新实现模块 num % base 和除法 num/base 操作。
【解决方案2】:

您是否考虑过编写自己的转换函数?这是一个可以调整为任意长度的工作示例。

警告:我的 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    • 2012-08-03
    • 2014-12-15
    • 2018-08-25
    • 2010-12-03
    • 1970-01-01
    相关资源
    最近更新 更多