PIC Microcontoller Radix Math Method

ASCII Hex to Binary

; From Regulus Berdin; untested
; Input  - ASCII number in W
; Output - binary in W

         sublw   '9'
         movlw   'A' - .10
         skpnc
          movlw  '0'
         subwf   ASCII,w

Tracy Smith says:

If you KNOW that the ASCII is '0'-'9','A'-'F' then a simpler solution for the midrange pics is:
    addlw   -'A'
    skpc
     addlw  'A' - 10 + '0'
    addlw   10

If the ASCII value is in ram, then this solution will work for the 12 bit core too:

    movlw   -'A'
    btfsc   ASCII,6
     movlw  -'0'
    addwf   ASCII,f   ;(or w)

Code:

See:

Peter Heinrich Says:

;;  Converts an ASCII character code (in W) into the integer value
;;  corresponding to the hexadecimal digit it represents.  '0'-'9'
;;  become 0x0-0x9; 'A'-'F' and 'a'-'f' become 0xa-0xf.  (This routine
;;  expects W to contain only valid hexadecimal digits.)  The result
;;  is returned in place in W.
;;
char2int:
   ; Shift the character.
   addlw    0x9f
   bnn      adjust               ; if positive, character was 'a' to 'f'
   addlw    0x20                 ; otherwise, shift to next range of digits
   bnn      adjust               ; if now positive, character was 'A' to 'F'
   addlw    0x7                  ; otherwise, character must have been '0' to '9'

adjust:
   addlw    0xa                  ; shift the result to account for the alpha offset
   andlw    0xf                  ; clamp the value to one nybble
   return

Comments:

Questions: