Searching \ for 'Binary to ASCII conversion' in subject line. ()
Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=binary+ascii+conversion
Search entire site for: 'Binary to ASCII conversion'.

Truncated match.
'Binary to ASCII conversion'
1999\03\19@083906 by

Hi !
I have a little problem. I should convert a 16 bit value to
ASCII-characters.

2 bytes  ->  '9' '9' '9' '9'   four separate ASCII-chars.

The 16 bit value can not be greater than 9999, so 4 digits will be enough.
Do you have any solution?

Greetigs,  Juha

juha tuomi wrote:
>
> Hi !
> I have a little problem. I should convert a 16 bit value to
> ASCII-characters.
>
> 2 bytes  ->  '9' '9' '9' '9'   four separate ASCII-chars.
>
> The 16 bit value can not be greater than 9999, so 4 digits will be enough.
> Do you have any solution?
>
> Greetigs,  Juha

Sucessive divisions by 10...

See the remainder for each divisions, push on stack, make a "counter" of
divisions until you have a zero on division...

Pop "counter" times, add 0x30, and print it!

Miguel

Just copy each nibble to a separate byte and OR with 0x30.

John Clark, Software Engineer
JohnCinter-intelli.com
(317) 715-8175 (voice & fax)

Interactive Intelligence, Inc.
3500 DePauw Blvd., Suite 1060
Indianapolis, IN  46268-1136
http://www.inter-intelli.com

> {Original Message removed}
> > Hi !
> > I have a little problem. I should convert a 16 bit value to
> > ASCII-characters.
> >
> > 2 bytes  ->  '9' '9' '9' '9'   four separate ASCII-chars.
> >
> > The 16 bit value can not be greater than 9999, so 4 digits will be enough.
> > Do you have any solution?
> >
> > Greetigs,  Juha

Do a BCD adjust to each nibble starting with the least significant
nibble.

After you've done this, you just need to seperate each nibble
and add the appropriate offset to nibble to get the ascii value.

Kind regards,

Stefan

juha tuomi wrote:
> Hi !
> I have a little problem. I should convert a 16 bit value to
> ASCII-characters.
> 2 bytes  ->  '9' '9' '9' '9'   four separate ASCII-chars.
> The 16 bit value can not be greater than 9999, so 4 digits will be enough.
> Do you have any solution?

decimal.  According to your text about that the 16 bits value can not be
greater than 9999, it looks like it is in bcd (binary coded decimal), is
that right?

If it is in decimal, well, just a nibble separation to the right, AND
0Fh, OR 30h, you get the ascii code for the digit.

if it is in hexadecimal, it is all wrong, first because 9999h (39321
decimal) will not fit in only 4 decimal digits.

Wagner

Hi,

Take a look at my website below.  There exists a 16 bit binary to BCD
conversion for 0 to 65535 using bcd div by 2 method.  The result is a
packed BCD.  You should process after to unpack it and convert it ASCII
by:

call    bin2bcd         ;convert to packed bcd

swapf   bcd2,w          ;get thousands in lower nibble
movwf   d1000           ;save result
movf    bcd2,w          ;get hundreds
movf    d100            ;save

swapf   bcd3,w          ;get tens in lower nibble
movwf   d1000           ;save result
movf    bcd3,w          ;get ones
movf    d100            ;save

But using John Payson's routine, which is much faster and the result is
already unpacked is the best way.  Adding 0x30 to the results will
convert it to ASCII easily.

;-------------------------------------------------------------------
;
; Binary-to-BCD.  Written by John Payson.
;
; Enter with 16-bit binary number in NumH:NumL.
; Exits with BCD equivalent in TenK:Thou:Hund:Tens:Ones.
;

org     \$0010   ;Start of user files for 16c84

NumH:   ds      1
NumL:   ds      1
TenK:   ds      1
Thou:   ds      1
Hund:   ds      1
Tens:   ds      1
Ones:   ds      1

Convert:                        ; Takes number in NumH:NumL
; Returns decimal in
; TenK:Thou:Hund:Tens:Ones
swapf   NumH,w
andlw   \$0F             ;*** PERSONALLY, I'D REPLACE THESE 2
addlw   \$F0             ;*** LINES WITH "IORLW 11110000B" -AW
movwf   Thou
movwf   Hund
movwf   Ones

movf    NumH,w
andlw   \$0F
movwf   Tens

swapf   NumL,w
andlw   \$0F

rlf     Tens,f
rlf     Ones,f
comf    Ones,f
rlf     Ones,f

movf    NumL,w
andlw   \$0F
rlf     Thou,f

movlw   \$07
movwf   TenK

; At this point, the original number is
; equal to
TenK*10000+Thou*1000+Hund*100+Tens*10+Ones ;
if those entities are regarded as two's
compliment ; binary.  To be precise, all of
them are negative ; except TenK.  Now the
number needs to be normal- ; ized, but this
can all be done with simple byte ; arithmetic.

movlw   \$0A                             ; Ten
Lb1:
decf    Tens,f
btfss   3,0
goto   Lb1
Lb2:
decf    Hund,f
btfss   3,0
goto   Lb2
Lb3:
decf    Thou,f
btfss   3,0
goto   Lb3
Lb4:
decf    TenK,f
btfss   3,0
goto   Lb4

retlw   0

regards,
Reggie

juha tuomi wrote:
{Quote hidden}

--
e-mail: rberdinbigfoot.com
ICQ#:   31651436
URL:    http://www.bigfoot.com/~rberdin

Hi,
the most efficient way is if you converts it to BCD beforehand. Then
follow the advice below. There is the AN526 for this. I use it. The
routine calls B16TOBCD.ASM.
Good luck!
Imre

On Fri, 19 Mar 1999, Clark, John wrote:

{Quote hidden}

> > {Original Message removed}

More... (looser matching)
- Last day of these posts
- In 1999 , 2000 only
- Today
- New search...