title: Not able to print hex bytes in C
body: <p>I know this question has already been asked many times. I am facing a problem where I am getting few <code>hex bytes</code> via <code>serial communication</code> in <code>ubuntu</code>. 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 <code>Docklight</code>. This software is giving me hex bytes and I need to receive it in my application.
  Hex bytes are like below:</p>
  
  <p><code>FF AA 2E 0F CC 0D</code></p>
  
  <p>I have connected both the systems via null modem cable. Now to print hex bytes, we need to include <code>%02X</code> in printf  statement. Doing this, it gave me a warning of <code>format ‘%X’ expects argument of type ‘unsigned int’, but argument 2 has type ‘unsigned char *’</code>. I searched about this and replaced  it with <code>%p</code>. Here is the code:</p>
  
  <pre><code>unsigned char buf[255];
  
  res = read(fd,buf,255);
  buf[res]='\0';
  
  printf("data: %p\n",buf);
  </code></pre>
  
  <p>The output of the printf statement is <code>data: 0x603380</code>. I opened the buf variable to see the exact values which are below:</p>
  
  <pre><code>Name : buf
  Details:"ÿª.\017Ì\n", '\0' &lt;repeats 248 times&gt;
  Default:0x603380 &lt;buf&gt;
  Decimal:6304640
  Hex:0x603380
  Binary:11000000011001110000000
  Octal:030031600
  </code></pre>
  
  <p>Now I really don't know why it shows random characters like <code>ÿª.\017Ì\n</code> The output of the printf is may be because it has combined all the bytes into one and displayed its hex equivalent. </p>
  
  <p>I actually need to save each byte like <code>FF</code> and <code>AA</code> 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 <code>int</code> or <code>strings</code>. Thanks.!</p>