How to format print for Hex like output

This is a discussion on How to format print for Hex like output within the C Programming forums, part of the General Programming Boards category; Hi, I have this little bit of code to print out some numbers from a characer array, the code below ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    6

    How to format print for Hex like output

    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]);} 
    
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Teropita View Post
    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[/CODE]
    If you mean for char to be unsigned, then you really should implicitly declare it as unsigned.

    I'm not sure why you have %h there. What's the h supposed to be doing? Use %0X if you want it to start with 0 on single digit numbers.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Thank you quzah,

    changing to unsigned fixed the leading FF problem.

    I'm very new to C programming, I didn't realise the leading FFs were related to the data type.

    I had %hX because when I only had %X I had even more leading characters.

    %0X did not add leading 0 on single digit numbers - but %02X did :-)


    Thanks Again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I print my output?
    By Hexadakota in forum C++ Programming
    Replies: 1
    Last Post: 01-22-2008, 04:29 PM
  2. output format
    By Beowolf in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2007, 01:22 AM
  3. Formatted output format suitable for printing
    By Boksha in forum Tech Board
    Replies: 6
    Last Post: 01-04-2007, 10:38 AM
  4. output format: screen vs .txt
    By -JM in forum C++ Programming
    Replies: 7
    Last Post: 04-20-2006, 09:05 PM
  5. format numbers output?
    By DanC in forum C++ Programming
    Replies: 4
    Last Post: 03-09-2006, 06:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21