Hi,
I have this little bit of code to print out some numbers from a characer array, the code below prints out:
FFAC 11 1 4F 48 45 FFAA F 0
I would like to format this for printing as:
AC 11 01 4F 48 45 AA 0F 00
basically I want to
1. suppress leafing FF - where does that come from anyway?
2. add a leading zero to any value under 0xF
In my real programme the values in the array change so its not just a matter of printing it out once correctly it needs to print correctly no matter the values in the array.
Thanks for any help you can provide in getting the print format correct.
Code:#include <sys/types.h> #include <ctype.h> #include <sys/stat.h> #include <stdio.h> #include <string.h> #include <stdlib.h> void main() { char dataarray[8]; int i; // Build up data to print via Array dataarray[0] = 172; //first octet of senders IP address in Hex eg 10 - 0A dataarray[1] = 17; //second octet of senders IP address in Hex eg 1 - 01 dataarray[2] = 1; //third octet of senders IP address in Hex eg 1 - 01 dataarray[3] = 79; //fourth octet of senders IP address in Hex eg 33 - 21 dataarray[4] = 0x48; // H in Hex dataarray[5] = 0x45; // E in Hex dataarray[6] = 0xAA; // AA dataarray[7] = 0x0F; //this value is tha datalength of the packet dataarray[8] = 0x00; //operate code: higher than 8 for (i=0;i<=8;i++) {printf("%hX ",dataarray[i]);} }