Thread: simple question about printing hex numbers at a certain size

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    5

    simple question about printing hex numbers at a certain size

    Say I had an array full of hex numbers that I wanted to print. If I just went ahead and printed them out one by one in a for loop like this:

    Code:
    for (a = 1; a<= 31; a++) {
        printf("%x\n",array[a]);
    }
    then the following might be displayed

    ffff0000
    0
    0
    0
    7fffef4c
    7fffef50
    0
    14
    19
    0
    0
    0
    0
    0
    0
    0
    c
    8
    fffffffb
    0
    0
    0
    0
    0
    0
    0
    0
    10008000
    7fffef48
    0
    400018

    My question is, how can I best edit the above code to make the program display each value as the same size (8 characters).
    Basically extended to the left by 0s if they aren't already 8 long.

    so like:

    ffff0000
    00000000
    00000000
    00000000
    7fffef4c
    ...
    so on

    Thanks

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Try this:

    Code:
    for (a = 1; a<= 31; a++) {
        printf("%08x\n",array[a]);
    }
    It will give you a 8 digit wide column left padded with zeroes.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    Works perfectly. Thankyou!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error with a vector
    By Tropicalia in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2006, 07:45 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. A simple printing question
    By Hexxx in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2006, 05:01 PM
  4. Hopefully simple question, input streams
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2006, 01:59 PM
  5. Newbie question on numbers.
    By thes in forum C++ Programming
    Replies: 14
    Last Post: 06-17-2002, 07:18 AM