优雅地打印 HEX 数据

每行每 8 个数据多增加一个空格,数据展示更加直观。

#define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')

/**
 * dump_hex
 * 
 * @brief dump data in hex format
 * 
 * @param buf: User buffer
 * @param size: Dump data size
 * @param number: The number of outputs per line
 * 
 * @return void
*/
void dump_hex(const uint8_t *buf, uint32_t size, uint32_t number)
{
    int i, j;

    for (i = 0; i < size; i += number)
    {
        printf("%08X: ", i);

        for (j = 0; j < number; j++)
        {
            if (j % 8 == 0)
            {
                printf(" ");
            }
            if (i + j < size)
                printf("%02X ", buf[i + j]);
            else
                printf("   ");
        }
        printf(" ");

        for (j = 0; j < number; j++)
        {
            if (i + j < size)
            {
                printf("%c", __is_print(buf[i + j]) ? buf[i + j] : '.');
            }
        }
        printf("\n");
    }
}

测试结果:

00000000:  00 01 02 03 04 05 06 07  08 09 0A 0B 0C 0D 0E 0F  ................
00000010:  10 11 12 13 14 15 16 17  18 19 1A 1B 1C 1D 1E 1F  ................
00000020:  20 21 22 23 24 25 26 27  28 29 2A 2B 2C 2D 2E 2F   !"#$%&'()*+,-./
00000030:  30 31 32 33 34 35 36 37  38 39 3A 3B 3C 3D 3E 3F  0123456789:;<=>?
00000040:  40 41 42 43 44 45 46 47  48 49 4A 4B 4C 4D 4E 4F  @ABCDEFGHIJKLMNO
00000050:  50 51 52 53 54 55 56 57  58 59 5A 5B 5C 5D 5E 5F  PQRSTUVWXYZ[\]^_
00000060:  60 61 62 63 64 65 66 67  68 69 6A 6B 6C 6D 6E 6F  `abcdefghijklmno
00000070:  70 71 72 73 74 75 76 77  78 79 7A 7B 7C 7D 7E 7F  pqrstuvwxyz{|}~.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C语言中使用串口接收hex数据,你可以使用串口通信库来完成这个任务。下面是一个简单的示例代码,演示如何接收并解析hex数据: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <termios.h> #define BUFFER_SIZE 1024 int main() { int serial_port = open("/dev/ttyS0", O_RDWR); // 打开串口设备 if (serial_port < 0) { perror("无法打开串口设备"); return -1; } struct termios tty; memset(&tty, 0, sizeof(tty)); if (tcgetattr(serial_port, &tty) != 0) { perror("无法获取串口属性"); return -1; } cfsetospeed(&tty, B9600); // 设置波特率为9600 cfsetispeed(&tty, B9600); tty.c_cflag &= ~PARENB; // 无奇偶校验位 tty.c_cflag &= ~CSTOPB; // 1位停止位 tty.c_cflag &= ~CSIZE; // 屏蔽字符大小位 tty.c_cflag |= CS8; // 8位数据位 tty.c_cflag &= ~CRTSCTS; // 禁用硬件流控制 tty.c_cc[VMIN] = 1; // 读取至少一个字节 tty.c_cc[VTIME] = 5; // 读取超时时间 if (tcsetattr(serial_port, TCSANOW, &tty) != 0) { perror("无法设置串口属性"); return -1; } char buffer[BUFFER_SIZE]; memset(buffer, 0, sizeof(buffer)); while (1) { int bytesRead = read(serial_port, buffer, sizeof(buffer)); // 读取串口数据 if (bytesRead > 0) { for (int i = 0; i < bytesRead; i++) { printf("%02X ", buffer[i]); // 打印hex数据 } printf("\n"); } } close(serial_port); // 关闭串口设备 return 0; } ``` 在这个示例代码中,我们使用了Linux系统的串口设备文件`/dev/ttyS0`,你可以根据实际情况修改为你所使用的串口设备文件。该代码打开串口设备并设置一些串口属性,然后不断地从串口读取数据打印hex格式的数据。 请注意,这只是一个简单的示例代码,你可能需要根据你的实际需求进行适当的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值