无法在C中打印十六进制字节 [英] Not able to print hex bytes in C

查看:281
本文介绍了无法在C中打印十六进制字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问过很多次了.我遇到的一个问题是,我在ubuntu中通过serial communication获得的hex bytes很少.我写了一个C代码,它读取所有数据并显示它.我已经用许多字符串对其进行了测试,所以效果很好.我也有另一个运行Windows的系统和一个名为Docklight的软件.该软件给了我十六进制字节,我需要在我的应用程序中接收它. 十六进制字节如下:

I know this question has already been asked many times. I am facing a problem where I am getting few hex bytes via serial communication in ubuntu. I have written a C code which reads all the data and display it. It is working fine as I have tested it with many strings. I also have another system running windows and a software named Docklight. This software is giving me hex bytes and I need to receive it in my application. Hex bytes are like below:

FF AA 2E 0F CC 0D

我已经通过零调制解调器电缆连接了两个系统.现在要打印十六进制字节,我们需要在printf语句中包含%02X.这样做会给我一个format ‘%X’ expects argument of type ‘unsigned int’, but argument 2 has type ‘unsigned char *’的警告.我搜索了一下,并用%p替换了它.这是代码:

I have connected both the systems via null modem cable. Now to print hex bytes, we need to include %02X in printf statement. Doing this, it gave me a warning of format ‘%X’ expects argument of type ‘unsigned int’, but argument 2 has type ‘unsigned char *’. I searched about this and replaced it with %p. Here is the code:

unsigned char buf[255];

res = read(fd,buf,255);
buf[res]='\0';

printf("data: %p\n",buf);

printf语句的输出为data: 0x603380.我打开了buf变量,以查看下面的确切值:

The output of the printf statement is data: 0x603380. I opened the buf variable to see the exact values which are below:

Name : buf
Details:"ÿª.\017Ì\n", '\0' <repeats 248 times>
Default:0x603380 <buf>
Decimal:6304640
Hex:0x603380
Binary:11000000011001110000000
Octal:030031600

现在我真的不知道为什么它显示像ÿª.\017Ì\n这样的随机字符.printf的输出可能是因为它将所有字节组合为一个并显示了其十六进制等效项.

Now I really don't know why it shows random characters like ÿª.\017Ì\n The output of the printf is may be because it has combined all the bytes into one and displayed its hex equivalent.

我实际上需要将每个字节(如FFAA)保存在数组中.但是就目前而言,我想到了要首先展示它.可能是什么问题.为什么它不显示十六进制字节.谁能给我建议.我还需要知道,当我们通过串行传输十六进制字节时,接收端应如何接收它.应将其另存为intstrings.谢谢!!

I actually need to save each byte like FF and AA in an array. But just for the time being, I thought of first displaying it. What could be the issue. Why it is not displaying hex bytes. Can anyone give me suggestions on this. I also need to know that when we transmit hex bytes via serial, how does the receiving end should receive it. It should be saved as int or strings. Thanks.!

推荐答案

这是一个基本的C问题,您要给printf()一个指针,并希望它显示您的十六进制转储,就好像它是一个字符串一样.那不是它的工作方式.

This is a basic C problem, you are giving printf() a pointer and expecting it to show your hex dump, as if it was a string. That's not the way it works.

您应该执行以下操作:

unsigned char buf[255];

res = read(fd,buf,255);
// AN HEX BUFFER IS NOT A STRING DO NOT TERMINATE IT
// buf[res]='\0';

printf("data: ");
for (int i = 0; i < res; ++i)
  printf("%02x ", (int)buf[i]);

printf("\n");

printf的选项%p 用于转储指针地址,而不是指针指向的内容.

The option %p of printf it's used to dump the pointer ADDRESS, not the contents a pointer point to.

这篇关于无法在C中打印十六进制字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆