Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert from dec to hex and save in hex

Status
Not open for further replies.

tcat72

Technical User
Oct 3, 2001
4
US
my overall goal is to sort hexidecimal numbers. My problem is converting from long int to hex and saving my data in hex format so that I may sort it in that format. For example:
long num = 1030507233;
How do I convert and save this? I know that the hex value is 3d6c4ea1 because I can print it out in hex format. How can I get and save this? I will also need to convert it back to decimal format once it has been sorted. This part shouldn't raise too much of a problem, but I'll appreciateany and all suggestions.
 
I have found the answer. I used the function itoa(). Here is an example:

#include <stdio.h>
#include <stdlib.h>

int main() {
char str[10]; /* MUST be big enough to hold all the characters of your number!! */

printf(&quot;15 in binary is %s\n&quot;, itoa(15, str, 2));
printf(&quot;15 in octal is %s\n&quot;, itoa(15, str, 8));
printf(&quot;15 in decimal is %s\n&quot;, itoa(15, str, 10));
printf(&quot;15 in hex is %s\n&quot;, itoa(15, str, 16));
return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top