## 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'
```

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'
```

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

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

```
+

• I read this last suggestion to use this instruction and ended up spending a few days trying to figure out why it is unpredictable. This does not work (at least not on a 12F629 *and* the assembler does not even generate a warning):

btfsc WREG,6 ; not all pics can do this...

That assembles to 1A00 == "btfsc 0, 6" and since W is a SFR (special function register 0) and NOT a File Register which btfsc operates on, it tests the correct bit in the wrong register. "btfsc 0,6" references INDF (File Register 0, the register pointed at by FSR), not W.

There is no place in the instruction encoding for this bit test instruction to specify the difference between WREG at 0 and INDF at 0. so that code "btfsc WREG, 6" really executes as "btfsc INDF, 6". Compact, but broken.

best regard from Barry L. Kramer in Mohnton PA
+

Questions:

This one does not care if the A-F is upper or lower case:

```hex2bin ; expects '0'-'9','A'-'F','a'-'f' in W
btfsc	WREG,6  ; not all PICs can do this...
addlw	9	; that's A-F (0x4? or 0x6?)
andlw	0x0F    ; we need only the low nibble
return
```

Yes, that's 3 instructions + the return :-)
Tamas
+

 file: /Techref/microchip/math/radix/ah2b-1d4b.htm, 7KB, , updated: 2009/12/8 10:51, local time: 2019/10/15 01:50, TOP NEW HELP FIND:  18.210.22.132:LOG IN

 ©2019 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?Please DO link to this page! Digg it! / MAKE! /  PIC Microcontoller Radix Math Methods ASCII Hex to Binary

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant" "No. But I'm interested. me at when this page is expanded."

.