Format Specifier %02x - Stack Overflow

    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs Format specifier %02x Ask Question Asked 11 years, 3 months ago Modified 5 years, 5 months ago Viewed 192k times 52

I have a simple program :

#include <stdio.h> int main() { long i = 16843009; printf ("%02x \n" ,i); }

I am using %02x format specifier to get 2 char output, However, the output I am getting is:

1010101

while I am expecting it to be :01010101 .

Share Improve this question Follow edited May 17, 2017 at 8:55 Mini Bhati's user avatar Mini Bhati 3435 silver badges20 bronze badges asked Aug 26, 2013 at 7:35 user2717225's user avatar user2717225user2717225 5311 gold badge4 silver badges5 bronze badges 0 Add a comment |

4 Answers 4

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 66

%02x means print at least 2 digits, prepend it with 0's if there's less. In your case it's 7 digits, so you get no extra 0 in front.

Also, %x is for int, but you have a long. Try %08lx instead.

Share Improve this answer Follow edited Aug 26, 2013 at 9:22 Chris Young's user avatar Chris Young 15.8k7 gold badges38 silver badges42 bronze badges answered Aug 26, 2013 at 7:38 aragaer's user avatar aragaeraragaer 17.8k6 gold badges50 silver badges52 bronze badges 4
  • 4 I have learnt that 'x' in '%x' refers to hexadecimal format and not int. Is it so? – Rohit Kiran Commented Jan 21, 2015 at 13:41
  • 18 x refers to "integer in hexadecimal format" as opposed to d which is "integer in decimal format". Both accept int as a value to be printed. lx and ld both accept long. – aragaer Commented Jan 22, 2015 at 0:28
  • 1 For the sake of completenes also read stackoverflow.com/questions/15108932/c-the-x-format-specifier and cplusplus.com/reference/cstdio/printf – randmin Commented Mar 22, 2018 at 23:12
  • "Both accept int as a value to be printed" --> Hmmm, "%x" is for unsigned, not int. Common enough that int "works" though, even if not specified by C. – chux Commented Jun 23, 2021 at 18:17
Add a comment | 18

%x is a format specifier that format and output the hex value. If you are providing int or long value, it will convert it to hex value.

%02x means if your provided value is less than two digits then 0 will be prepended.

You provided value 16843009 and it has been converted to 1010101 which a hex value.

Share Improve this answer Follow edited Jun 21, 2019 at 8:48 leiyc's user avatar leiyc 94311 silver badges23 bronze badges answered Nov 3, 2017 at 6:13 PulkitRajput's user avatar PulkitRajputPulkitRajput 6697 silver badges8 bronze badges Add a comment | 2

Your string is wider than your format width of 2. So there's no padding to be done.

Share Improve this answer Follow answered Aug 26, 2013 at 7:37 PP.'s user avatar PP.PP. 10.9k7 gold badges47 silver badges59 bronze badges Add a comment | -3

You are actually getting the correct value out.

The way your x86 (compatible) processor stores data like this, is in Little Endian order, meaning that, the MSB is last in your output.

So, given your output:

10101010

the last two hex values 10 are the Most Significant Byte (2 hex digits = 1 byte = 8 bits (for (possibly unnecessary) clarification).

So, by reversing the memory storage order of the bytes, your value is actually: 01010101.

Hope that clears it up!

Share Improve this answer Follow edited Feb 19, 2016 at 19:54 Ahmed Akhtar's user avatar Ahmed Akhtar 1,4631 gold badge17 silver badges29 bronze badges answered Feb 19, 2016 at 18:24 RJM's user avatar RJMRJM 714 bronze badges 2
  • Uh no, printf is not printing the MSB to the right, that would be pretty confusing – eckes Commented May 17, 2017 at 8:59
  • 3 Besides what eckes said, bit numbering inside of most processors isn't really a thing because you generally deal with a byte at a time. Bit ordering matters only for serialization. Endianness (big or little) is byte ordering, not bit ordering. – Daniel Papasian Commented Aug 6, 2017 at 16:09
Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • We'll Be In Touch - A New Podcast From Stack Overflow!
  • The app that fights for your data privacy rights
  • Featured on Meta
  • More network sites to see advertising test
  • We’re (finally!) going to the cloud!
  • Call for testers for an early access release of a Stack Overflow extension...

Linked

101 C - The %x format specifier -1 I can't understand "String.format() function in this example any help ?" 1 C format specifier question 5 C format specifier 1 Format Specifier in C 2 printf unknown specifier %S 5 C "%d" format specifier 20 %p Format specifier in c 0 format specifier value issue in c 0 C: Dynamic format specifier 3 Printf format specifiers 1 Printing with format specifiers in C

Hot Network Questions

  • User temp folder is 103GB; is it safe to delete?
  • What is the origin of the term "Dog Character" in the context of fighting games?
  • Was it really possible to damage my VGA card by programming it in assembly through its latches registers?
  • safe divide command for pgfmathparse to enable equality checks involving division by zero
  • Low Resolution in Org Latex Preview
  • Lowest processable signal level after FFT with given noise level
  • 1980s or 90s space cartoon with a space prince and princess
  • Do hypotheses need a “how” explanation or are predictions enough to validate them?
  • Suggestion for catching a flight with short layover in Amsterdam
  • Why can't I exclude my answer environment?
  • Do rediscoveries justify publication?
  • Importing a .tak file into Blender
  • Understanding Linux 'top' command: Memory vs Swap display format confusion
  • What are the ethical considerations regarding mandatory class participation?
  • What does one contemplate to become a sotāpanna?
  • Biasing common-source NMOS with active load and fixed Vgs
  • Quantum gravity and perturbative parameters
  • Does Windows 11 Pin Behavior Break Password Security Conventions?
  • Is my evaluation for this multiple linear regression correct?
  • "Your move, bud."
  • Mind the gap between……
  • Is using a virtualised android instance or emulator within "operating system vendor supported or warranted configurations"?
  • Why do some people write text all in lower case?
  • Can I pretend that Spearman's = Pearson's correlation coefficients for meta analysis?
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-c

Từ khóa » C 0x 02x