-3

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?

user2378481
  • 789
  • 1
  • 9
  • 17

2 Answers2

1
  1. Call the input number X.

  2. Call a string S that is initially blank.

  3. Compute X mod 16.

  4. 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'.

  5. Replace string S with the character you just got followed by the current contents of string S.

  6. Divide X by 16 (using integer division), store the result in X

  7. If X is not zero, go to step 3.

  8. 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