; 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)
I think the correct conversion code from Tracy Smith should read : addlw -'A' ; expect upper case letters skpc addlw 'A' - 0x18 -'0' addlw 0x11+
| bcf 0x00,5 | movlw 0xC9 | addwf 0x00,f | movlw 0x27 | btfss 0x03,0 | addwf 0x00,f | return+
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
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 :-)
|file: /Techref/microchip/math/radix/ah2b-1d4b.htm, 7KB, , updated: 2009/12/8 10:51, local time: 2018/3/21 11:44,
|©2018 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?|
<A HREF="http://www.piclist.com/techref/microchip/math/radix/ah2b-1d4b.htm"> PIC Microcontoller Radix Math Methods ASCII Hex to Binary</A>
|Did you find what you needed?|
PICList 2018 contributors:
o List host: MIT, Site host massmind.org, Top posters @20180321 RussellMc, Isaac M. Bavaresco, Sean Breheny, Van Horn, David, Neil, alan.b.pearce, Bob Blick, David C Brown, John Gardner, Brent Brown,
* Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.
Welcome to www.piclist.com!