Thread: Forcing a "leading 0" on 2 bit hexidecimal values

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    19

    Forcing a "leading 0" on 2 bit hexidecimal values

    If I'm printing a hex value ala...

    Code:
    printf("%x", number)
    and it only turns out to be anything from 0 to F, how can I force a leading 0?

    So it gets printed out as 09, 0F etc.

    So it's always 2 bit hex, but shouldn't print out the 0 if it's anything above F (10, 1A, 4F etc.)

    I tried %2x, that just seemed to put spaces in. :-\

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Code:
    printf("%0x", number); /* prefix a zero to the conversion character */

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Quote Originally Posted by itCbitC View Post
    Code:
    printf("%0x", number); /* prefix a zero to the conversion character */
    Quote Originally Posted by Evenstevens View Post
    but shouldn't print out the 0 if it's anything above F (10, 1A, 4F etc.)
    So, it is not resolving his problem

  4. #4
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    So, it is not resolving his problem
    Yes, but the OP can use the information to arrive at a solution.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    @OP: use sprintf first, and store the hexadecimal of 'number' in a string buffer, than revise if the length of the string is 1, if so, add a '0'.

  6. #6
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    This can be done easily with a little bit of condition checking,
    and using
    Code:
    printf("%02X",number);
    .
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    2 bit hex value can store numbers 0,1,2,3 only
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  2. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 08:02 PM
  3. bit patterns of negtive numbers?
    By chunlee in forum C Programming
    Replies: 4
    Last Post: 11-08-2004, 08:20 AM
  4. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM