Bob Blick wrote: > Does anyone have a nice code snippet that takes a byte containing a hex > character (0x30 through 0x39 and 0x41 through 0x46) and converting it to a > plain numeric byte, 0x00 through 0x0F? Everything that comes to mind seems > wasteful and clunky. > > The character coming in may also contain junk that must be filtered out. The following code is from the UARTTEST program available on me web site. It also handles 'a' to 'f' (lower case), although you could remove that. Eric ;----------------------------------------------------------------------------- ; test whether character in W is a hex digit ; returns with STATUS.Z clear if not hex ; returns with STATUS.Z set if hex, and value in W ;----------------------------------------------------------------------------- ; 0-9 A-F a-f Z ; $30-39 $41-$46 $61-$66 $4a ishex: addlw 0c6h ; $f6-$ff $07-$0c $27-$2c $10 addlw 00ah ; $00-$09 $11-$16 $31-$36 $1a btfsc STATUS,C goto is09 addlw 0e9h ; $fa-$ff $1a-$1f $03 addlw 006h ; $00-$05 $20-$25 $09 btfsc STATUS,C goto isaf addlw 0dah ; $fa-$ff $e3 addlw 006h ; $00-$05 $e9 btfsc STATUS,C goto isaf addlw 061h ; $4a bcf STATUS,Z return isaf: addlw 00ah is09: bsf STATUS,Z return