# Thread: need function to convert int to hex

1. ## need function to convert int to hex

I am loading some integers and long integers into a string as hexidecimal. For example:

int I 1234;
str char[2];
Then funct(I) returns:
str[0]=0x04 and str[1]=0xD2

I have written the expectable verbose routine that loops through dividing by powers of 16, generates the hex digits and stores them one by one into the string. However can someone point me to some "nice" compact way to do this?

Thanks,
Fritz

2. I know we're not supposed to hand out code but in this case it's just way too tempting...

Code:
```void DecToHexStr(int dec, char *str)
{ sprintf(str, "%X", dec); }```
Just make sure your string buffer has enough characters to store the full 32 bit returned value plus an extra for the null terminator... thus char hex[9];

3. Haha....I had to 'like' that one.

4. Originally Posted by AndrewHunter
Haha....I had to 'like' that one.
LOL... some days you gets the bear, some days the bear gets you...

5. Ah-didn't know about the code rule-sorry.

Thanks for the answer-works fine! I wouldn't have thought of using a "print" command to convert data but if the shore fits---------

Cheers
Fritz

6. Originally Posted by sonnichs
Ah-didn't know about the code rule-sorry.
No worries, Tater was just making a joke.

Originally Posted by sonnichs
Thanks for the answer-works fine! I wouldn't have thought of using a "print" command to convert data but if the shore fits---------

Cheers
Fritz
If that is what you were looking for. I guess in this case, you need to wrap your head around the concept that decimal, hex, octal, ect., are all just representations of numbers. There is no such thing as a decimal type. The types of int, double, float, ect. just define the binary format for how the number is stored. The actual representation of an int as a decimal or hex, is just the graphical representation of that binary format. Hence, yes "print" commands would work quite well.

7. "The actual representation of an int as a decimal or hex, is just the graphical representation of that binary format"

I really like that way of expressing it!

I used to write mostly in assembler and I always felt spoiled becuase you knew exactly what you stuffed into registers and "core" and it stayed that way. I don't write much code these days and I forget a lot of the conversions that C does when you perform mixed type operations. I sometimes lay awake at night wondering what is going to over flow, underflow etc because I forgot to check "signed" vs "unsigned", roundoff and so on. As Tater's motto ends: TEST----

Cheers
Fritz

8. Originally Posted by sonnichs
As Tater's motto ends: TEST----
Tater still hasn't figured out that you can't just keep adding more -- to the end to keep subtracting one more! That's not how you subtract two! Plus, he forgot his ; at the end. Silly tater!

Quzah.

9. LOL...now we are moving on to C syntax humor. Quzah you really are an odd one

10. Originally Posted by AndrewHunter
Quzah you really are an odd one
LOL... And you are only now figuring that out????

You might say his number is 41 ... just short of the answer to life, love, the universe, everything.

11. Originally Posted by CommonTater
LOL... And you are only now figuring that out????

You might say his number is 41 ... just short of the answer to life, love, the universe, everything.
And now you have learned about the prefix operator!

Quzah.

12. Originally Posted by quzah
And now you have learned about the prefix operator!
Quzah.
Haha.....but I do have to ask, what happened to all the colorful posts?

13. Originally Posted by quzah
And now you have learned about the prefix operator!
Quzah.
Code:
```if (quzah)
--credibility;```
You mean like that?

14. more like:
Code:
```if(quzah)
ExitWindowsEx(EWX_SHUTDOWN, SHTDN_REASON_MAJOR_APPLICATION);```

15. Originally Posted by AndrewHunter
more like:
Code:
```if(quzah)
ExitWindowsEx(EWX_SHUTDOWN, SHTDN_REASON_MAJOR_APPLICATION);```
From "The Jackel"... "Da good guys don't hide"...