0

i have some problems to understand the following:

I will implement a real small protocol in C. For negotiation server and client have to exchange information via hex values.

When I answer the request from the client I send following array:

char array[] = { 0x00, 0x02, 0x01, 0x2F, 0x02 }

The calculated length with sizeof(array) is 5. For debugging purposes I did a printf of the array.

for( i = 0; i < length; i++ ){
    printf("%x", *(array + i));
}

The output is 0212F2. How the client knows which values belongs together? There are more possiblities, aren't there? Maybe 0x00, 0x21, 0x02, 0x0F, 0x02, but that is not correct.

Can someone explain this?

Thanks a lot:)

Florian

florian2840
  • 49
  • 3
  • 10
  • Is there a reason you're not just talking in plain bytes instead of hex strings? – Matti Virkkunen May 13 '15 at 21:25
  • Your output is printing "0", "2", "1", "2F" and "2". Looks OK to me :) If you want it to print "0x02" instead of "2", change your format specifier to "0x%02x". – FoggyDay May 13 '15 at 21:29
  • If you want to send this data as a text encoding, *you* need to provide the encoding to know how to reassemble them on reception. A leading `'0'` char for anything less than `0x10` (16) and ensuring the receiver pulls the chars two-per-octet during reassembly will probably work for you. But I concur with Matti regardless. Just send the array content with a length-preamble and call it a day. – WhozCraig May 13 '15 at 21:31
  • BTW: recommend sending prefix and postfix `char` with you message like "!0002012F02\n". _Far_` easier to cope with scant, long, tardy or otherwise disturbed messages. – chux - Reinstate Monica May 13 '15 at 21:35
  • 1
    I'm not sure whether you want to transmit binary values (would be 5 bytes) or hexadecimal ascii string (would be 10 bytes, where eg. 0 becomes binary 0x3030). Could you clarify? – JeffRSon May 13 '15 at 21:39

3 Answers3

2

First declare your array as an unsigned char array. char can be signed or unsigned depending on the implementation and in your case you want the unsigned case.

Then use:

   printf("%02hhx", array[i]);

It will print a leading 0 if necessary:

0002012F02
ouah
  • 142,963
  • 15
  • 272
  • 331
  • 1
    Note: if using `"%02hhx"`, code could used `unsigned char`, `char` or even `signed char`. – chux - Reinstate Monica May 13 '15 at 21:31
  • See also [What is the purpose of the h and hh modifiers for printf?](http://stackoverflow.com/questions/4586962/what-is-the-purpose-of-the-h-and-hh-modifiers-for-printf). My recommendation remains: just send a byte at a time (here, 5 characters), and only worry about "0x%02x" (or whatever format you prefer) when you want to *display* the bytes in the array. – FoggyDay May 13 '15 at 23:00
0

The client prints your array in order of i as you loop, which is the order of your content in the array (unless im misunderstanding the question). If you're asking why its prints it in that order, that's because its the order you defined in the array and increment the array index one value at a time.

Javia1492
  • 862
  • 11
  • 28
0

The client will get the bytes in the exact order you send them. Which will be:

0x00
0x02
0x01
0x2F
0x02

Using a new line (\n) and the %02x suggested by ouah together it will be easier to see. Right now it's not clear because all the information is lumped together.

printf("%02x\n", *(array+i));
Dom
  • 1,687
  • 6
  • 27
  • 37