Exact match. Not showing close matches.
PICList
Thread
'[PIC]: 32bit binary to ASCII conversion'
2000\06\08@064801
by
Henrik Holmgrd
Hi all
I have a project where I have to convert a 32 bit binary or hex number to
ASCII to be displayed on a LCD. Does some one on this list have done this
before, if so are you willing to share the solution with me or give me some
hints. I have done decimal to ASCII conversion no problem, but now my
project end up with a 32 bit number and then I have only very complicated
solutions.
tnx in advance.
\||||||/
_____oo0o__( o o )__o0oo_____
(_)
Henrik HolmgŒrd
2000\06\08@073145
by
John Perkinton
I remember having to so this for integrating an ip address to a pic chip.
look for a project on the net called the worlds smallest web server. it uses
a seiko s7600a to connect a 16f84 to the internet. I'm sure it has some good
example code for dealing with 32bit addresses. if i remember right. I will
try to find the stuff I did myself and post it up soon.
{Original Message removed}
2000\06\08@103330
by
Thomas McGahee
|
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}
2000\06\08@121317
by
jamesnewton
2000\06\09@080911
by
Bob Ammerman
There is a simple trick, outlined in the following pseudo-code for doing
this conversion:
ANSWER - an ascii decimal number - 10 digits
SOURCE - a 32-bit binary number
ANSWER <- '000000000'
for I = 1 to 32
ANSWER = ANSWER + ANSWER
if high bit of SOURCE is set then
ANSWER = ANSWER + 1
end if
SOURCE = SOURCE + SOURCE
next i
Note that the arithmetic on 'ANSWER' is done directly on the ASCII
representation.
For example, to compute ANSWER + ANSWER:
carry = 0
for digit = 0 to 9
temp = ANSWER[digit]+ANSWER[digit]+carry-'0'
carry = 0
if (temp > '9') then
temp = temp - 10
carry = 1
end if
ANSWER[digit] = ANSWER[digit]+ANSWER[diit
next digit
Bob Ammerman
RAm Systems
(high function, high performance, low-level software)
More... (looser matching)
- Last day of these posts
- In 2000
, 2001 only
- Today
- New search...