By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,301 Members | 1,434 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,301 IT Pros & Developers. It's quick & easy.

print hex in n-bit format

P: n/a
printf("Hex = %#x\n", 28); will print out 0x1c

What if I want to print 0x001c for 16-bits?, or even 0x0000001c with
32-bits?

can i adjust the format in my case?

please advice. thanks!!

Sep 29 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
John wrote:
printf("Hex = %#x\n", 28); will print out 0x1c

What if I want to print 0x001c for 16-bits?, or even 0x0000001c with
32-bits?

can i adjust the format in my case?
#include <stdio.h>
#include <limits.h>
int main (void)
{
printf("Hex = %0#6x\n", 28);
#define NIBBLE_FMT(e) (sizeof (e) * CHAR_BIT + 3)/4+2, (e)
printf("Hex = %0#*x\n", NIBBLE_FMT(28));
return 0;
}

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 29 '06 #2

P: n/a

John wrote:
printf("Hex = %#x\n", 28); will print out 0x1c

What if I want to print 0x001c for 16-bits?, or even 0x0000001c with
32-bits?

can i adjust the format in my case?

please advice. thanks!!
use a "field width" to specify the length. A leading 0 before the width
will insert 0's instead of spaces, so

printf("%#06x",28)

prints 0x001c, whereas

printf("%#010x)

can be used for 32 bits.

Hubble.

Sep 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.