piclist 2000\06\08\103330a >
Thread: 32bit binary to ASCII conversion
www.piclist.com/techref/microchip/devices.htm?key=pic
BY : Thomas McGahee email (remove spam text)

Henrik,

You have to convert the 32 bit binary number to a decimal
form and then convert the decimal form to ascii by adding
30h to each decimal digit.

I will run through the example for an 8 bit binary conversion
just to show you the principles involved. Using pseudo-code:

bin_to_dec:
;assume that upon entry 8 bit number is in bin_holder.
;assume that decimal result will be placed in dec3, dec2, dec1
;where dec3 is most significant decimal digit.
;uses temp holder <decnum>.
;upon exit bin_holder will be trashed.
CLEAR dec1 ;clear all decimal holders to zero.
CLEAR dec2
CLEAR dec3
ROTATE bin_holder RIGHT INTO CARRY ;bit0 <1> handled first...
IF CARRY=1
SET decnum=1
CALL ADD_ONES
ENDIF
ROTATE bin_holder RIGHT INTO CARRY ;bit1 <2> handled next...
IF CARRY=1
SET decnum=2
CALL ADD_ONES
ENDIF
ROTATE bin_holder RIGHT INTO CARRY ;bit2 <4> handled next...
IF CARRY=1
SET decnum=4
CALL ADD_ONES
ENDIF
ROTATE bin_holder RIGHT INTO CARRY ;bit3 <8> handled next...
IF CARRY=1
SET decnum=8
CALL ADD_ONES
ENDIF
ROTATE bin_holder RIGHT INTO CARRY ;bit4 <16> handled next...
IF CARRY=1
SET decnum=1
CALL ADD_TENS
SET decnum=6
CALL ADD_ONES
ENDIF
ROTATE bin_holder RIGHT INTO CARRY ;bit5 <32> handled next...
IF CARRY=1
SET decnum=3
CALL ADD_TENS
SET decnum=2
CALL ADD_ONES
ENDIF
ROTATE bin_holder RIGHT INTO CARRY ;bit6 <64>
IF CARRY=1
SET decnum=6
CALL ADD_TENS
SET decnum=4
CALL ADD_ONES
ENDIF
ROTATE bin_holder RIGHT INTO CARRY ;bit7 <128>
IF CARRY=1
SET decnum=1
CALL ADD_HUNDREDS
SET decnum=2
CALL ADD_TENS
SET decnum=8
CALL ADD_ONES
ENDIF
RETURN

ADD_ONES:
ADD decnum to dec1
IF dec1 > 9
SUBTRACT 10d from dec1
GOTO ADD_TENS
ENDIF
RETURN

ADD_TENS:
ADD decnum to dec2
IF dec2 > 9
SUBTRACT 10d from dec2
GOTO ADD_HUNDREDS
ENDIF
RETURN

ADD_HUNDREDS:
ADD decnum to dec3
IF dec3 > 9
SUBTRACT 10d from dec3
GOTO ADD_TENS
ENDIF
RETURN

*** END OF PROGRAM ***

There are many ways to improve upon this code, but
this code fragment should give you an idea of what the
basic algorithm is like. For a full 32 bit implementation
you would need ADD routines for thousands, tens_thousands,
hundreds_thousands, millions, all the way up to
thousand_millions (billions to some of us).

That means 32 code segments to handle the 32 bits, and
10 decimal ADD routines.

Note that any decimal carry required is handled by the
GOTOs. This method is easy to understand, but in reality it
would be more efficient to implement the decimal additions
using indirect addressing via the FSR. That cuts the code
SIZE down a lot.

Note that the decimal ADD routines can also be used by
other parts of your program

Fr. Tom McGahee

{Original Message removed}
<011b01bfd156\$f9cfc8c0\$c07cfea9@tom> 8bit

See also: www.piclist.com/techref/microchip/devices.htm?key=pic
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) 32bit binary to ASCII conversion

new search...