c++: need help converting a decimal number to hex

Started by
10 comments, last by phil05 19 years, 7 months ago
What header file / code would I use to output a decimal number in hex?
Advertisement
try:
printf("%x", myint);
As your leader, I encourage you from time to time, and always in a respectful manner, to question my logic. If you're unconvinced that a particular plan of action I've decided is the wisest, tell me so, but allow me to convince you and I promise you right here and now, no subject will ever be taboo. Except, of course, the subject that was just under discussion. The price you pay for bringing up either my Chinese or American heritage as a negative is - I collect your f***ing head.
Thanks, however, it just seems to print the LSD (least significant digit) instead of the full hex representation aka 0x00000001. Instead it's only 1. Any way around this?
hmmm.... how about:
	printf("%.8x", myint);
As your leader, I encourage you from time to time, and always in a respectful manner, to question my logic. If you're unconvinced that a particular plan of action I've decided is the wisest, tell me so, but allow me to convince you and I promise you right here and now, no subject will ever be taboo. Except, of course, the subject that was just under discussion. The price you pay for bringing up either my Chinese or American heritage as a negative is - I collect your f***ing head.
	int q = 20;	cout << std::hex << q << endl;


Wasn't there a thread about you recently?
Yep it worked. Now it's just not showing 0x before the numbers. I could use cout to put it in, but if any better way, please let me know. Thanks for the help.
yeah, you'll have to put the "0X" in yourself, it is C/C++ code to signify a hex number, not a universal way of displaying hex.
As your leader, I encourage you from time to time, and always in a respectful manner, to question my logic. If you're unconvinced that a particular plan of action I've decided is the wisest, tell me so, but allow me to convince you and I promise you right here and now, no subject will ever be taboo. Except, of course, the subject that was just under discussion. The price you pay for bringing up either my Chinese or American heritage as a negative is - I collect your f***ing head.
Okay, I guess it doesn't matter. I'm just using c++ for pseudocode for an assembly assignment.
Quote:Original post by philvaira
I'm leaving this unhelpful site.


Actually, IIRC cout provides a mechanism to print the preceding 0x, but I can't remember exactly what it was...



[Edited by - evolutional on September 14, 2004 4:28:53 AM]
lol... I was in a bitchy mood at the time.

This topic is closed to new replies.

Advertisement