title: Convert an 8bit number to hex in z80 assembler
body: <p>I am writing a game for the ZX Spectrum using z80 and have a bit of a problem.
  I have manipulated a routine to convert a number held in the “a” register to a hex value held in “de”. I’m not sure of how to convert the other way, EG pass in a hex value in de and convert this to decimal held in “a”.</p>
  
  <p>NB: The following routine converts the input to the ascii values that represent the values 0 through to F. EG if a = 255 then d =70 and e = 70 as “F” is ascii value 70.</p>
  
  <pre><code>NumToHex    ld c, a   ; a = number to convert
              call Num1
              ld d, a
              ld a, c
              call Num2
              ld e, a
              ret  ; return with hex number in de
  
  Num1        rra
              rra
              rra
              rra
  Num2        or $F0
              daa
              add a, $A0
              adc a, $40 ; Ascii hex at this point (0 to F)   
              ret
  </code></pre>
  
  <p>Can anyone advise on a solution to work this in reverse or offer a better solution?</p>