Searching \ for 'How to convert an 8 bit byte into two ASCII chars.' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=how+convert+bit
Search entire site for: 'How to convert an 8 bit byte into two ASCII chars.'.

Truncated match.
PICList Thread
'How to convert an 8 bit byte into two ASCII chars.'
1998\06\11@050532 by Clewer,Brian

flavicon
face
Hi all,

I am currently building an X.25 protocol analyser and I have most of it
done.  All I need to do now is send the data to my PC.  My problem is
that I have an 8 bit byte and I need to convert it into two printable
chars to send to my PC.  E.g..  00111001 would be 39 (in hex).  I need to
send the two chars 3 and 9 to my PC.  Does anyone have any ideas?

Thanks in advance,

Brian.

1998\06\11@062334 by James Cameron

picon face
Clewer,Brian wrote:
> My problem is that I have an 8 bit byte and I need to convert it
> into two printable chars to send to my PC.  E.g..  00111001 would be
> 39 (in hex).  I need to send the two chars 3 and 9 to my PC.

       move the byte to W
       swap nibbles (swapf)
       and with 0x0f
       or with 0x30
       send that byte

       move the byte to W again
       and with 0x0f
       or with 0x30
       send that byte

Hey, I enjoyed that.  Uh oh.  ;-)

--
James Cameron                              (spam_OUTjames.cameronTakeThisOuTspamdigital.com)
Digital Equipment Corporation (Australia) Pty. Ltd. A.C.N. 000 446 800

1998\06\11@070350 by Clewer,Brian

flavicon
face
James Cameron  wrote:

>Clewer,Brian wrote:
>> My problem is that I have an 8 bit byte and I need to convert it
>> into two printable chars to send to my PC.  E.g..  00111001 would be
>> 39 (in hex).  I need to send the two chars 3 and 9 to my PC.
>
>        move the byte to W
>        swap nibbles (swapf)
>        and with 0x0f
>        or with 0x30
>        send that byte
>
>        move the byte to W again
>        and with 0x0f
>        or with 0x30
>        send that byte
>
>Hey, I enjoyed that.  Uh oh.  ;-)

This is ok if you use the numbers 0 to 9 but it doesn't work if you need
HEX values from A to F.

Any more ideas?

Thanks,
Brian.

1998\06\11@084845 by James Cameron

picon face
Clewer,Brian wrote:
> >        move the byte to W
> >        swap nibbles (swapf)
> >        and with 0x0f
> >        or with 0x30
> >        send that byte
> >
> >        move the byte to W again
> >        and with 0x0f
> >        or with 0x30
> >        send that byte
> This is ok if you use the numbers 0 to 9 but it doesn't work if you
> need HEX values from A to F.
> Any more ideas?

Yeah, you're right.  Good observation.

Okay, time to factor out the nibble to hex ASCII digit stuff into
it's own subroutine ...

hex                             ; convert nibble to ascii hex
       andlw   0x0f            ; keep only the right bits
       movwf   tmp1            ; save it for later
       sublw   0x09            ; subtract from nine
       movf    tmp1,w          ; bring it back (leave carry alone)
       btfsc   status,c        ; test result of subtraction
                               ; (maybe it's btfss, not sure!)
       addlw   0x37            ; adjust to A-F
       return
hex_0
       addlw   0x30            ; adjust to 0-9
       return

byte
       movwf   tmp2            ; save the byte for later
       swapf   tmp2,w          ; grab the high nibble back
       call    hex             ; encode high nibble as hex ascii
       call    send            ; send the byte in w to serial port
       movf    tmp2,w          ; fetch the byte again
       call    hex             ; encode low nibble
       call    send            ; send the byte in w to serial port
       return

I've not tested that.
It's probably wrong somewhere.
I'm a beginner.
Did it work?  ;-)

--
James Cameron                              (.....james.cameronKILLspamspam@spam@digital.com)
Digital Equipment Corporation (Australia) Pty. Ltd. A.C.N. 000 446 800

1998\06\11@085000 by Thomas McGahee

flavicon
face
Brian,
IF the original nibble is <= 9 then OR it with 30H to
get ASCII "0" - "9" exactly as James Cameron showed you.

IF the nibble has a hex value from 0AH to 0FH then
subtract 9 and OR with 40H.

For example, if the nibble
is 0AH then subtracting 9 will give you 01H, and ORing
with 40H will give you 41H. If you desire lower case
instead of upper case, then OR with 60H instead.

BTW, you can "subtract" 9 by adding the two's complement
of 9, which is F7H

Hope this helps.
Fr. Tom McGahee

----------
{Quote hidden}

1998\06\11@102034 by Eric Belanger

picon face
It should be easy. What you nedd is a BCD to ASCII conversion routine.
Here the base idea:

1 - Check the uper 4 bits, a AND mask with 0xF0 and a swap should do.
2 - Check if the result is lower than or equal to 0x09
3 - If so, just add 0x30, the ASCII value of "0" and you have
   your first digit.
4 - If the number is in the 10 - 15 range add 0x31 so you will
   end with an ASCII char between "A" and "F"
5 - Change the mask to 0x0F to keep the 4 lower bits and repeat steps
   2 to 4.

Hope this may help.


>From EraseMEowner-piclistspam_OUTspamTakeThisOuTmitvma.mit.edu Thu Jun 11 03:34:54 1998
>Received: from PEAR.EASE.LSOFT.COM (206.241.12.19) by
grape.EASE.LSOFT.COM (LSMTP for OpenVMS v1.1b) with SMTP id
<13.F4B0240Bspamspam_OUTgrape.EASE.LSOFT.COM>; Thu, 11 Jun 1998 5:04:32 -0500
>Received: from MITVMA.MIT.EDU by MITVMA.MIT.EDU (LISTSERV release 1.8c)
with
>          NJE id 4580 for @spam@PICLISTKILLspamspamMITVMA.MIT.EDU; Thu, 11 Jun 1998
05:05:32
>          -0400
>Received: from MITVMA (NJE origin SMTP@MITVMA) by MITVMA.MIT.EDU (LMail
>          V1.2b/1.8b) with BSMTP id 1157; Thu, 11 Jun 1998 05:04:56
-0400
>Received: from tukdss1.uk.telematics.com (147.137.5.30) by
mitvma.mit.edu (IBM
>          VM SMTP V2R3) with TCP; Thu, 11 Jun 98 05:04:54 EDT
>Received: from tarkus by tukdss1.uk.telematics.com (5.x/SMI-SVR4) id
AA19316;
>          Thu, 11 Jun 1998 10:01:41 +0100
>Received: by tarkus with Microsoft Mail id <35800DC8@tarkus>; Thu, 11
Jun 98
>          10:03:04 PDT
>Encoding: 12 TEXT
>X-Mailer: Microsoft Mail V3.0
>Message-ID:  <35800DC8@tarkus>
>Date:         Thu, 11 Jun 1998 10:00:00 PDT
>Reply-To:     pic microcontroller discussion list
<KILLspamPICLISTKILLspamspamMITVMA.MIT.EDU>
>Sender:       pic microcontroller discussion list
<RemoveMEPICLISTTakeThisOuTspamMITVMA.MIT.EDU>
>From:         "Clewer,Brian" <spamBeGoneBrian.ClewerspamBeGonespamTELEMATICS.COM>
>Subject:      How to convert an 8 bit byte into two ASCII chars.
>To:           TakeThisOuTPICLISTEraseMEspamspam_OUTMITVMA.MIT.EDU
>
>Hi all,
>
>I am currently building an X.25 protocol analyser and I have most of it
>done.  All I need to do now is send the data to my PC.  My problem is
>that I have an 8 bit byte and I need to convert it into two printable
>chars to send to my PC.  E.g..  00111001 would be 39 (in hex).  I need
to
>send the two chars 3 and 9 to my PC.  Does anyone have any ideas?
>
>Thanks in advance,
>
>Brian.
>


______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com

1998\06\11@102037 by Sean Breheny

face picon face
On Thu, 11 Jun 1998, Clewer,Brian wrote:

{Quote hidden}

You are correct, it only works for 0-9. To make it work for the rest, try
this: instead of just OR'ing with 0x30, check to see if the nibble is 10
or higher (check bit 3 to see if it is at least 8, then see if bits 2 or
1 are on). If it is ten or higher, add 0x41, otherwise, OR with 0x30.
Remember, don't OR with 0x41, ADD it.

There may also be a more elegant solution which I am not seeing right
now, but I am at work and need to get back to what I am SUPPOSED to be doing!

Good luck,

Sean





> Any more ideas?
>
> Thanks,
> Brian.
>

1998\06\11@102052 by Marco DI LEO

flavicon
face
Brian,
you can try this sequence if you need to convert a byte in the
corresponding character hexadecimal representation:

       swapf   value, W         ; Get the value nibble-swapped
       andlw   0x0f             ; Isolate the low nibble
       addlw   -0x0a            ; Check for digit value
       btfsc   STATUS, C        ; If 0-9 skip next
       addlw   0x07             ; Add offset between digits and
                                ; letters (use 0x27 for lowercase)
       addlw   0x3a             ; Add back the value we subtracted
                                ; for test and the offset between
                                ; 0 and the char '0'

       (here W holds the high nibble char)

       movf    value, W         ; Get the value in W
       andlw   0x0f             ; Same as high digit
       addlw   -0x0a
       btfsc   STATUS, C
       addlw   0x07
       addlw   0x3a

       (here W holds the low nibble char)

(I am sorry if the comments are unclear but I it is a little hard for me
to explain better since English is not my native language.)

Ciao
 Marco

Thomas McGahee wrote:
{Quote hidden}

1998\06\11@102055 by myke predko

flavicon
face
In an earlier discussion, we came up with the quick way of converting a
nybble in "w" to a hex byte:

NybbletoASCII                   ;  Convert a Nybble in "w" to ASCII

 addlw  0x036                  ;  Add '0' + 6 to Value
 btfsc  STATUS, DC             ;  If Digit Carry Set, then 'A' - 'F'
  addlw 7                      ;   Add Difference Between '9' and 'A'
 addlw  0-6

 return                        ;  Return the ASCII of Digit in "w"

To convert a byte, this subroutine can be called from:

BytetoASCII                     ;  Convert a Byte to two ASCII Bytes using
                               ;   "NybbletoASCII"

 movwf  Temp                   ;  Save the Byte to Convert for Lower Nybble

 swapf  Temp,w                 ;  Move the High Nybble into "w"
 andlw  0x00F
 call   NybbletoASCII
 movwf  FirstByte              ;  Save the Byte Returned

 movf   Temp,w
 andlw  0x007                  ;  Convert the Low Nybble
 call   NybbletoASCII
 movwf  SecondByte

 return

Converting ASCII to a byte is an exercise left to the reader.

myke

{Quote hidden}

"In this life we all get an equal share of ice.  The rich get it in the
summer, the poor get it in the winter." - Bat Masterson's last words

1998\06\11@180925 by Mike Keitz

picon face
On Thu, 11 Jun 1998 11:50:00 PDT "Clewer,Brian"
<RemoveMEBrian.ClewerEraseMEspamEraseMETELEMATICS.COM> writes:
>James Cameron  wrote:
>
>>Clewer,Brian wrote:
>>> My problem is that I have an 8 bit byte and I need to convert it
>>> into two printable chars to send to my PC.  E.g..  00111001 would
>be
>>> 39 (in hex).  I need to send the two chars 3 and 9 to my PC.

[non-working routine removed]

>Any more ideas?

Here's what I use.  It's rather generic.  You need a temporary register
"tmp" and a subroutine called "serout" that sends W to the PC.  Enter by
calling "serout2dig" with the number in W.  It will send two ASCII digits
that represent the number in hexadecimal.  Note the conversion routine
uses the ADDLW instruction, which isn't present on 12-bit PICs.

;------
serout2dig
; Outputs W as 2 hexadecimal or packed BCD digits.  The high 4 bits are
;  output first so they appear leftmost on the terminal screen.
;  Uses tmp.  On exit, a copy of the original W is in tmp.
       movwf   tmp
       swapf   tmp,w           ;Get first (high) digit.
       call    seroutdig
       movfw   tmp             ;Get second (low) digit.
; FALL THRU to seroutdig.
;------
seroutdig
; Outputs low 4 bits of W as a BCD or hexadecimal digit.  High 4 bits
ignored.
; W is erased.  Exits via serout.
       andlw   b'00001111'     ;Keep only low 4 bits.
       addlw   0 - .10         ;Less than 10?
       skpnc                   ;Yes, OK.
       addlw   'A' - ('9' + 1) ;If >9, fix as a letter.
       addlw   '0' + .10       ;Offset back up to ASCII.
       goto    serout

_____________________________________________________________________
You don't need to buy Internet access to use free Internet e-mail.
Get completely free e-mail from Juno at http://www.juno.com
Or call Juno at (800) 654-JUNO [654-5866]

1998\06\11@190034 by Jorge Ferreira

flavicon
face
At 11:50 98.06.11 PDT, you wrote:
{Quote hidden}

       Well, maybe its worth to take a look at the following code snipset.
       I extracted the idea from an old program, and wrote these pieces in a
hurry, but I think they might be usefull.


       [] Jorge F


: --- Table generation: Generates a retlw table for the translation from
hex(4bit) to ASCII
HEX_ASC_TAB     macro
       local   i=0
       while   i < 10
               retlw   i
       i+=1
       endw
       local i=65  ; Change 65 to 97
       while i<71  ; and 71 to 103 if you want lowercase letters
               retlw   i
       i+=1
       endw
               endm


[snip]

; ---- Translation routine
Convert addwf   pc,f
               HEX_ASC_TAB


[snip]

; ---------  Convertion
; IN_BYTE -> data to be converted
; OUT_ASC_1 -> Converted high nibble
; OUT_ASC_2 -> Converted lowe nibble


...
       swapf   IN_BYTE,W               ; get high nibble in W
       andlw   0x0f
       call    Convert         ; get ASCII in W
       movwf   OUT_ASC_1               ; store result
       movlw   0x0f                    ; get low nibble in W
       andwf   IN_BYTE,W
       call    Convert         ; get ASCII in W
       movwf   OUT_ASC_2               ; store result
...



===============================================================
cumprimentos / best regards
     Jorge Ferreira          //RemoveMEjorgegfspam_OUTspamKILLspammail.telepac.pt
------ Make sure brain is in gear before engaging mouth -------
===============================================================

1998\06\12@022228 by Dr. Imre Bartfai

flavicon
face
On Thu, 11 Jun 1998, Clewer,Brian wrote:

{Quote hidden}

Instead of

iorlw   30h     ; or with 0x30

you should write the following:

Version A.

call    nibble
...
nibble: addwf   pcl
       retw    '0123456789ABCDEF'
....

Version B.
       addlw   246
       skpnc
       addlw   7
       addlw   58
....
now you have it!
(all numbers are decimal unless noted otherwise!)

Imre

1998\06\12@022624 by Dr. Imre Bartfai

flavicon
face
Sorry, you are wrong!
Why?

Due to weird sublw because of after sublw you have
0 if you had 9
and 9 if you had 0. So the result will be bogus.

Imre


On Thu, 11 Jun 1998, James Cameron wrote:

{Quote hidden}

1998\06\12@052913 by STEENKAMP [M.ING E&E]

flavicon
picon face
Hi,

What about:
   movlw   66h         ;If digits are > 9 then DC and/or C will be set
   addwf   DataByte, W
   movlw   30h         ;Default to digit being 0..9
   btfsc   STATUS, DC  ;Is digit > 9?
    movlw  37h         ;Yes, init with 'A'-10
   movwf   LSChar      ;Least significant digit
   movlw   30h         ;Default to digit being 0..9
   btfsc   STATUS, C   ;Is digit > 9?
    movlw  37h         ;Yes, init with 'A'-10
   movwf   MSChar      ;Most significant digit

   movf    DataByte, W
   andlw   0Fh         ;Isolate lower digit
   addwf   LSChar, F

   swapf   DataByte, W
   andlw   0Fh         ;Isolate upper digit
   addwf   MSChar, F



{Quote hidden}

The above btfsc will only skip the addlw and still do the return.  You
will never get to hex_0

{Quote hidden}

Niki

1998\06\12@063424 by Stuart Allen

flavicon
face
What about a table like this, add the individual nibbles to PCL and get the
conversion back in W.


SWAPF    VAL, W        ; VAL is byte to display
CALL    NIB2HEXTAB
; W=High nibble char here
MOVF     VAL, W
CALL    NIB2HEXTAB
; W=Low nibble char here



NIB2HEXTAB
ANDLW    0FH
ADDWF    PCL, F
DT    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
'E', 'F'


I think the codes OK, but either way, the ideas simple enough.



Stuart.

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