I am trying to display a decimal as a hex. If I have a decimal such as 3703484078, how can I use fputc to display it as hex DCBEAEAE? Is there some formula that I could use?
Asked
Active
Viewed 779 times
2 Answers
1
Call the input number X.
Call a string S that is initially blank.
Compute X mod 16.
Convert the answer you got from the previous step to a character using the following mapping: 0->'0' 1->'1' 2->'2' 3->'3' 4->'4' 5->'5' 6->'6' 7->'7' 8->'8', 9->'9' 10->'A' 11->'B' 12->'C' 13->'D' 14->'E' 15->'F'.
Replace string S with the character you just got followed by the current contents of string S.
Divide X by 16 (using integer division), store the result in X
If X is not zero, go to step 3.
Output string S from left to right using
fputc
.

David Schwartz
- 179,497
- 17
- 214
- 278
0
You will have to convert the number to a string of hexadecimal text digits.
Search StackOverflow for "hex conversion" or "hex printing" for details.

Thomas Matthews
- 56,849
- 17
- 98
- 154