%#08x Formatting Of Printf | C Programming | Coding Forums

Coding Forums
  • Forums New posts Search forums
  • Members Current visitors
Log in Register What's new Search

Search

Everywhere Threads This forum This thread Search titles only By: Search Advanced search…
  • New posts
  • Search forums
Menu Log in Register Install the app Install
  • Forums
  • Archive
  • Archive
  • C Programming
You are using an out of date browser. It may not display this or other websites correctly.You should upgrade or use an alternative browser. %#08x formatting of printf
  • Thread starter Bilgehan.Balban
  • Start date Jan 26, 2006
B

Bilgehan.Balban

Hi, I use %#08x to print unsigned integers in hexadecimal format. According to C ref. man. Harbison & Steele, #08 stands for "pad the number with up to 8 zeroes to complete it to 8 digit number". Is this correct understanding? However this is not always the case. I sometimes see 4, sometimes 2, that does not complete the whole number into 8 digits. I don't know what changes this, but do you have an alternative that definitely pads to 8 digits? Thanks, Bahadir R

Robert Harris

Hi, I use %#08x to print unsigned integers in hexadecimal format. According to C ref. man. Harbison & Steele, #08 stands for "pad the number with up to 8 zeroes to complete it to 8 digit number". Is this correct understanding? However this is not always the case. I sometimes see 4, sometimes 2, that does not complete the whole number into 8 digits. I don't know what changes this, but do you have an alternative that definitely pads to 8 digits? Thanks, Bahadir Click to expand...
The # is a flag prefixing "0x" to the result The 8 is the minimum field width (but some of the field may consist of spaces before the "0x"). You really need a precision of 8 and a field width of 10 (to include the "0x", so you need: "%#10.8x" Robert H

Henryk

No time to check, but as I recall I used something like printf("%08x", ....) or printf("0x%08", ...) for the prefix. I never used the # tag. J

Jordan Abel

No time to check, but as I recall I used something like printf("%08x", ....) or printf("0x%08", ...) for the prefix. I never used the # tag. Click to expand...
That's because the # tag doesn't apply to a value of zero. B

Bilgehan.Balban

Robert said: The # is a flag prefixing "0x" to the result The 8 is the minimum field width (but some of the field may consist of spaces before the "0x"). You really need a precision of 8 and a field width of 10 (to include the "0x", so you need: "%#10.8x" Robert Click to expand...
Thanks, I'll try this. Bahadir C

Chris Torek

Almost. See below ...
Robert Harris said: (e-mail address removed) wrote: The # is a flag prefixing "0x" to the result The 8 is the minimum field width (but some of the field may consist of spaces before the "0x"). You really need a precision of 8 and a field width of 10 (to include the "0x", so you need: "%#10.8x" Click to expand...
Both of these are correct but incomplete. As Jordan Abel pointed out elsethread, the "#" flag ("alternate form") only applies the prefix is the number is nonzero (for both octal and hex conversions, in fact). ("Alternate form" has different meanings for floating-point formats as well.) The "0" in "08" is also a flag, meaning "pad with zeros instead of blanks" (for numeric conversions -- the effect is undefined for conversions like %s). To include a zero flag, it must (obviously) appear before a numeric field width: "%09d" means "pad with zeros, 9 wide, decimal" but "%90d" means "pad with blanks, 90 wide, decimal". The zero flag only applies for right-justified fields; if you specify both the "-" flag and the "0" flag, the "0" flag is ignored: "%-9d" and "%-09d" mean the same thing (as does "%0-9d"). Using a precision, as in Robert Harris' example above, is roughly equivalent to specifying the zero flag. But it is not exactly the same. For instance: printf("5.2d: >%5.2d<\n", 5); printf("05d: >%05d<\n", 5); prints lines with "> 05<" and ">00005<" respectively. Using "%#10.8x" will do the Right Thing for all nonzero numbers: printf("*%#10.8x*\n", 0x4321); /* prints *0x00004321* */ but for 0 you get: printf("*%#10.8x*\n", 0); /* prints * 00000000* */ Note that the 0x has disappeared (as required for "%#x" format), so 10.8 -- a field width of 10, with a precision of 8 -- now pads with blanks. You could leave out the field width ("%#.8x"), but then nonzero numbers will occupy 10 character fields (8 digits plus the leading 0x) while zero will use 8 character (8 digits, no leading 0x). You could even write "%#010.8x" and hope for zero padding when the 0x is omitted, but this seems ... wrong. :) The two "best" alternatives are likely "0x%.8x" or "0x%08x", which will do exactly the same thing. Post reply

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

printf() formatting - stripping zeroes, padding 0 Oct 19, 2004
printf() formatting - stripping zeroes, padding 4 Oct 19, 2004
fprintf slower than printf and redirect? 1 Nov 29, 2008
update: timezone offset calc and date formatting 0 Apr 8, 2005
comp.lang.c FAQ list Table of Contents 0 Jan 12, 2008
comp.lang.c Answers to Frequently Asked Questions (FAQ List) 15 Apr 1, 2006
comp.lang.c FAQ list Table of Contents 0 Jan 1, 2006
In the Matter of Herb Schildt: a Detailed Analysis of "C: TheComplete Nonsense" 109 Apr 3, 2010
Share: Facebook Twitter Reddit Pinterest Tumblr WhatsApp Email Share Link

Members online

No members online now. Total: 272 (members: 2, guests: 270) Robots: 45

Forum statistics

Threads 474,017 Messages 2,570,297 Members 46,918 Latest member AgnesMqr87

Latest Threads

  • Javascript set language function issue
    • Started by crypt_net010
    • Today at 4:54 PM
  • Return pointer from void only gives the memory address
    • Started by Steffen595
    • Today at 2:49 AM
  • Flutter web
    • Started by Jean-Marie
    • Yesterday at 11:56 PM
  • Strange result using "for(in... " iteration
    • Started by jimandy
    • Yesterday at 7:38 PM
  • Multithreaded Server Under Load: Real-Time Packet Exchange with 5 Clients
    • Started by Infinityhost
    • Friday at 7:32 PM
  • Python and HTML
    • Started by HTMLpigeon64
    • Friday at 7:29 PM
  • Urgent Help Needed: Supporting Education and Family Through Hard Work
    • Started by miantsafanirina
    • Friday at 8:26 AM
  • Accountability intro
    • Started by bauble
    • Thursday at 9:44 AM
  • I want to convert Outlook data files to MBOX manually
    • Started by Techykaus24
    • Thursday at 8:33 AM
  • Gray Scott model in FEniCS
    • Started by Lonny Petersen
    • Wednesday at 7:13 AM
  • Forums
  • Archive
  • Archive
  • C Programming
Top

Từ khóa » C 0x 08x