user123454321
user123454321

Reputation: 152

What is the meaning of this format `%02hhx`?

I saw in some functions that in order to convert number to hexadecimal format, using this format: printf("%02hhx", some_char); , but I don't understand why, and what is the meaning of this format ?

Upvotes: 1

Views: 3023

Answers (1)

Gerhardh
Gerhardh

Reputation: 12404

Format string "%02hhx" means:

  • 0: Results that would require less than specified field width shall be printed with leading 0s.
  • 2: use at least 2 characters to format the value
  • x Print in hexadecimal format.
  • hh The provided parameter for x is only a char, not an int For values 0..127 it doesn't matter if you add the hh but values above could get sign extended and would be printed with lots of leading F.

The result will just be a hexadecimal value with 2 digits.

Upvotes: 4

Related Questions