【问题标题】:How to convert char to int and then display it in hex format如何将char转换为int,然后以十六进制格式显示
【发布时间】:2015-03-18 12:24:07
【问题描述】:

我正在用 c 语言编写一个程序,在该程序中我从串行设备获取数据并将其存储在缓冲区 char receivebuffer[100] 中。当我显示接收缓冲区的内容时,输出显示(这是 ASCII 格式吗)。但预期的输出是十六进制格式。如何将其转换为十六进制?

我还想知道,如果我将缓冲区转换为 int,输出是否相同。请告诉我如何将 char 缓冲区也转换为 int?

#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int set_interface_attribs (int fd, int speed, int parity)
{
    struct termios tty;
    memset (&tty, 0, sizeof tty);
    if (tcgetattr (fd, &tty) != 0)
    {
            printf("error %d from tcgetattr\n\n", errno);
            printf("Error Opening the device\n\n");
            exit(0);
            //error_message ("error %d from tcgetattr", errno);
            return -1;
    }

    cfsetospeed (&tty, speed);
    cfsetispeed (&tty, speed);

    tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
    // disable IGNBRK for mismatched speed tests; otherwise receive break
    // as \000 chars
    tty.c_iflag &= ~IGNBRK;         // disable break processing
    tty.c_lflag = 0;                // no signaling chars, no echo,
                                    // no canonical processing
    tty.c_oflag = 0;                // no remapping, no delays
    tty.c_cc[VMIN]  = 0;            // read doesn't block
    tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

    tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl

    tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
                                    // enable reading
    tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
    tty.c_cflag |= parity;
    tty.c_cflag &= ~CSTOPB;
    tty.c_cflag &= ~CRTSCTS;

    if (tcsetattr (fd, TCSANOW, &tty) != 0)
    {
            printf("error %d from tcsetattr\n\n", errno);
            printf("Error Opening the device\n\n");
            exit(0);
            //error_message ("error %d from tcsetattr", errno);
            return -1;
    }
    return 0;
}

void set_blocking (int fd, int should_block)
{
    struct termios tty;
    memset (&tty, 0, sizeof tty);
    if (tcgetattr (fd, &tty) != 0)
    {
            printf("error\n\n");
            printf("Error Opening the device\n\n");
                    exit(0);
            //error_message ("error %d from tggetattr", errno);
            return;
    }

    tty.c_cc[VMIN]  = should_block ? 1 : 0;
    tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

    if (tcsetattr (fd, TCSANOW, &tty) != 0)
        printf("Error Opening the device\n\n");
        //error_message ("error %d setting term attributes", errno);
}

int main()
{
char *portname = "/dev/ttyUSB0";

int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0)
{
    //error_message ("error %d opening %s: %s", errno, portname,         strerror (errno));
    printf("error");

}

set_interface_attribs (fd, B9600, 0);  // set speed to 115,200 bps, 8n1 (no parity)
set_blocking (fd, 0);                // set no blocking

       // send 7 character greeting

usleep ((7 + 25) * 100);             // sleep enough to transmit the 7    plus
while(1)
{

char receivebuffer [100];
read (fd, receivebuffer, sizeof receivebuffer);  // read up to 100    characters if ready to read

printf("value of buffer is %s\n\n", receivebuffer);
return 0;
}
}

【问题讨论】:

  • 请贴出代码,因为 convert 完全没有意义,是不是显示出来了?
  • 在我看来,printf() 中的转换说明符错误,那么发布一个 sn-p 呢?
  • @SouravGhosh 我期待十六进制格式的数据,但它是……
  • @iharob 是的,我想以十六进制显示。
  • 点击编辑并发布,你需要将每行缩进4个空格加上你的缩进。

标签: c hex ascii


【解决方案1】:

您需要将接收到的字节数存储在某处并在 for 循环中使用它,像这样尝试

char receivebuffer[100];
int  count;
int  i;
count = read (fd, receivebuffer, sizeof receivebuffer);  // read up to 100 characters if ready to read
for (i = 0 ; i < count ; ++i)
 {
    printf("0x%02X ", receivebuffer[i]);
    if ((i + 1) % 8 == 0)
        printf("\n");
 }

这个if ((i + 1) % 8 == 0)只是连续打印8字节,你可以更改或删除它,但它有助于检查数据。

【讨论】:

    【解决方案2】:

    你应该替换这个:

    printf("value of buffer is %s\n\n", receivebuffer);
    

    与:

    for (int tmpfoo = 0; receivebuffer[tmpfoo] != '\0'; tmpfoo++)
    {
        printf("value of buffer is %X\n\n", (int)receivebuffer[tmpfoo]);
    }
    

    如果您希望它只是由 HEXvalue 跟随 HEXvalue。

    【讨论】:

    • 感谢您的帮助。 %X 是十六进制的吗?如果我想要十进制或二进制。?
    • 遗憾的是无法提供二进制,而对于十进制,只需使用 %d 并注意转换为 int。
    • 太棒了。谢谢你
    • 为什么是tmpfoo &lt; sizeof(receivebuffer)?并假设receivebuffer 不是nul 终止,这几乎可以肯定。获取read() 返回的值并准确打印该字节数不是更简单吗?还假设缓冲区中有一个嵌入的'\0'?什么?因此,如果我可以投两次反对票,我会的。
    • @iharob 那么他的整个代码都是错误的。他使用了%s,这让我期待,因为他没有提到任何错误,所以数组是空终止的。否则他会以不属于他的问题的方式遇到麻烦,应该是一个新职位。所以我可以放心地期待它以 null 终止。
    【解决方案3】:

    您不需要转换它。要将char 显示为十六进制数字,请在printf 函数组中使用%hhx 格式。

    【讨论】:

    • 我已经这样做了,但它向我显示警告:“格式‘%hhx’需要‘int’类型的参数,但参数3的类型为‘char *’”??
    • %hhx 用于单个char,而不用于char*
    猜你喜欢
    • 2011-11-27
    • 2011-10-02
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    相关资源
    最近更新 更多