## 4 Bit Binary to 1 ASCII HEX digit

Converting a Nybble to ASCII

This is a question that comes up all the time when the contents of a byte are to be displayed/output. Does your code need to be fast, or does your code need to be small?

## 4 Bit Binary to 1 ASCII HEX digit FAST

The most obvious and fastest way of doing this is:

```NybbletoASCII:
; Call with binary nybble in W. Returns ASCII value in W.
; From: http://www.myke.com/basic.htm
andlw H'0F' ; (optional )
addwf  PCL, f            ;  Add the Contents of the Nybble to PCL/
dt	 "0123456789ABCDEF"  ;  return the ASCII as a Table Offset
```

## 4 Bit Binary to 1 ASCII HEX digit SMALL

Several people have independently reduced the core of nybble-to-ASCII conversion to 4 instructions in size, zero RAM. Can you spot that core inside these 6 instruction subroutines?

```NybbletoASCII:
; from Sean H. Breheny [shb7 at CORNELL.EDU]
; Sean H. Breheny KA3YXM [at rocket-roar.com]
andlw H'0F' ; (optional )
addlw 0-D'10'               ; test: Is W < 10 ?
skpnc ; btfsc STATUS,C  ; If 0 <= W <= 9, skip ahead
addlw 'A'-'0'-D'10' ; If A <= W <= F, add ASCII char 'A', and
; subtract enough to make the next
; line have no effect
; replace the original 10 that was subtracted
return
```

Another:

```NybbletoASCII:
; From: Scott Dattalo
andlw H'0F' ; (optional )
skpndc
return
```

## example

All of the above routines are interchangeable. Some test code that calls those routines with all possible values of W:

```
test_nybble_to_ascii:
; for( i = 0; i < 256; i++){ nybble_to_ascii(i) };
movlw h'00' ;
movwf index
nybble_test_for_loop:
; nybble_to_ascii(i);
movf index,W
call nybble_to_ascii
nop ; place to put breakpoint to see if it converted correctly
nybble_test_for_test:
decfsz index,f
goto nybble_test_for_loop
; ... continue
return

```

• Nevermind, the ASCII table is in a slightly different order than I was thinking. I see.+
• Dear Piclist Keepers,

I just wanted to say a big "THANKS!" for all the great help, tips, and first hand knowledge you provide. I've been able to get my PIC projects running much better with your help.

Take care, and Thanks,
baumgarc AT gps.caltech.edu+

Questions:

• hvymtlsteve-Remove- at charter.net asks: " Can't you convert a 4-bit nybble to ASCII by simply adding 0x30 to it?" James Newton replies: No, because the ASCII values above "9" are ":", ";", "<", "=", ">", and "?" rather than "A" thru "F". To convert a nibble to hex, you need to add "A"-10 (55 or 0x37) if it is 10 (0x0A or 1010b) or more and "0" (48 or 0x30) if it is 9 (0x09 or 1001b) or less. Or you can add "0" as you suggest, then test to see if it is ":" or greater and then add 7 more ("A" - ":") if it is.+

+

 file: /Techref/microchip/math/radix/b2a-4b1a.htm, 4KB, , updated: 2008/2/11 15:46, local time: 2021/12/7 07:58, TOP NEW HELP FIND:  54.158.251.104:LOG IN

 ©2021 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 Microcontroller Radix Math Method 4 Bit Binary to 1 ASCII HEX digit

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 a nice message (short messages are blocked as spam) 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."

 PICList 2021 contributors: o List host: MIT, Site host massmind.org, Top posters @20211207 Neil, Harold Hallikainen, Alan Pearce, RussellMc, Bob Blick, Allen Mulvey, Justin Richards, Dwayne Reid, madscientistatlarge, Sean Breheny, * 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.

.