Gordon Couger wrote:
{Quote hidden}>
> -----Original Message-----
> From: Justin Grimm <
.....eclipseKILLspam
.....ACCESSIN.COM.AU>
>
> >Hi piclisters
> >I need a short routine that will convert a single byte
> >to 3 ascii bytes
> >
> >For example; if the number 127 (decimal) is in file x,
> >how can I convert it to 3 registers (hundreds,tens and units).
> >This is what the 3 files should have in them after the conversion
> > register1 = 31, register2 = 32, register3 = 37 (all in ascii
> >notation)
>
> a = 127
> b = a mod 10 +48
> a = a /10
> c = a mod 10 + 48
> a = a /10
> d = a+48
> Is the way it is done. The bad part is the number is in reverse order. The
> way I do it is to start at the right hand side of the string and return a
> pointer
> to the first character.
I was going to point you to the Archive, but for some reason it stops at
April '98. Here's something that I posted last May. It only converts the
numbers to BCD, but going to ascii is trivial. (Does any one have this
stuff referenced on a web page)?
--------------------------------------------------------------
Here's a 24-instruction solution with a little looping:
Explanation:
anick.simplenet.com/piclist/Oct97/0369.html
Code:
http://anick.simplenet.com/piclist/Oct97/0312.html
It's based on what Payson affectionately calls his 'wonderful 16-bit
binary to BCD algorithm':
www.iversoft.com/cgi-bin/lwgate/PICLIST/archives/March97/date/article-0.h
tml
(This link is from the archive and takes a while to download.
It's been a year since it's been posted, perhaps it's time
again?)
Here's the 28-cycle (non-looping) version that I promised yesterday.
It's based on binary comparisons. It's one of those routines that
is very difficult to comment. So I didn't. However it takes advantage
of this little trick to quickly ascertain the ones' digit:
If you look at the ones' digit for 2^N you see this pattern:
n = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 ...
2^n % 10 = 1, 2, 4, 8, 6, 2, 4, 8, 6, 2, 4, 8 ...
If it wasn't for the annoying 6's, you could simply sum the nibbles to
get and get the ones' digit (after a relatively simple binary to BCD
conversion). However, it's simple enough to test if bit 4, 8,...
are high and to subtract 1 and then add 6 (or simply add 5) if they
are.
The second observation is that the sum of all of the tens' digits is
less than 16, and thus can fit into one nibble. This simplifies having
to deal with overflow until after all of the digits have been added
together.
The third observation is that the BCD result is greater than 200 only
if the most significant bit is set.
;********************************
;binary_to_bcd - 8-bits
;
;Input
; bin - 8-bit binary number
;Outputs
; hundreds - the hundreds digit of the BCD conversion
; tens_and_ones - the tens and ones digits of the BCD conversion
binary_to_bcd:
CLRF hundreds
SWAPF bin,W
ADDWF bin,W
ANDLW 00001111b
SKPNDC
ADDLW 0x16
SKPNDC
ADDLW 0x06
ADDLW 0x06
SKPDC
ADDLW -0x06
BTFSC bin,4
ADDLW 0x16 - 1 + 0x6
SKPDC
ADDLW -0x06
BTFSC bin,5
ADDLW 0x30
BTFSC bin,6
ADDLW 0x60
BTFSC bin,7
ADDLW 0x20
ADDLW 0x60
RLF hundreds,F
BTFSS hundreds,0
ADDLW -0x60
MOVWF tens_and_ones
BTFSC bin,7
INCF hundreds,F
Scott