Searching \ for '[PIC]: ASCII to HEX' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/microchip/devices.htm?key=pic
Search entire site for: 'ASCII to HEX'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: ASCII to HEX'
2002\02\04@102427 by Royce Simmons

picon face
Hi,

I am looking for a ASCII to HEX conversion assembler routine.  I need to
convert 2 ASCII
characters to a single hex digit.

Thanks for any help,  Royce

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email spam_OUTlistservTakeThisOuTspammitvma.mit.edu with SET PICList DIGEST in the body


2002\02\04@105759 by Peter Onion

flavicon
face
On 04-Feb-02 Royce Simmons wrote:
> Hi,
>
> I am looking for a ASCII to HEX conversion assembler routine.  I need to
> convert 2 ASCII
> characters to a single hex digit.

All you ever needed to know is at....

http://www.piclist.com/techref/microchip/math/radix/index.htm


It only takes 12 instructions.....  Maybe you need to find a simpler project to
start with ?

Read, Understand & Learn from it....

Peter.

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email .....listservKILLspamspam@spam@mitvma.mit.edu with SET PICList DIGEST in the body


2002\02\04@141853 by Andrew Warren

flavicon
face
Royce Simmons <PICLISTspamKILLspammitvma.mit.edu> wrote:

> I am looking for a ASCII to HEX conversion assembler routine.  I need
> to convert 2 ASCII characters to a single hex digit.

Royce:

Enter with your two ASCII characters in CHAR1 (tens' digit) and CHAR2
(ones' digit).  Exits with CHAR1 and the CHAR2 modified, and the hex
value in W.

   LIST R = DEC

   MOVLW   '0'
   BTFSC   CHAR1,6
   MOVLW   'A'-10
   SUBWF   CHAR1

   MOVLW   '0'
   BTFSC   CHAR2,6
   MOVLW   'A'-10
   SUBWF   CHAR2

   SWAPF   CHAR1,W
   ADDWF   CHAR2,W

-Andy

=== Andrew Warren -- .....aiwKILLspamspam.....cypress.com
=== Principal Design Engineer
=== Cypress Semiconductor Corporation
===
=== Opinions expressed above do not
=== necessarily represent those of
=== Cypress Semiconductor Corporation

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email EraseMElistservspam_OUTspamTakeThisOuTmitvma.mit.edu with SET PICList DIGEST in the body


2002\02\04@153753 by Bob Barr

flavicon
face
On Mon, 4 Feb 2002 11:18:56 -0800, Andrew Warren wrote:

{Quote hidden}

Royce,

Andy's code here will work if you need the value expressed in BCD and
not its actual hex value. I'm not entirely clear on which one you
want.

If you need its true hex value, you'll  need to multiply the msb
digit's numeric value by 10 (decimal) before adding it to the lsb's
numeric value. (The SWAPF of CHAR1 actually multiplies it by 16.)

In either case, you can remove the BTFSC's and the MOVLW's following
them since the BTFSC will always skip the MOVLW. Bit 6 of your digit's
ASCII will always be clear for decimal digits '0' through '9'.

Regards, Bob

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservspamspam_OUTmitvma.mit.edu with SET PICList DIGEST in the body


2002\02\04@173007 by Andrew Warren
flavicon
face
Bob Barr <@spam@PICLISTKILLspamspammitvma.mit.edu> wrote:

> Andy's code here will work if you need the value expressed in BCD and
> not its actual hex value.
> ....
> If you need its true hex value, you'll  need to multiply the msb
> digit's numeric value by 10 (decimal) before adding it to the lsb's
> numeric value.

   Oops, you're right.  The correct code:

   LIST R = DEC

   MOVLW   '0'
   BTFSC   CHAR1,6
   MOVLW   'A'-10
   SUBWF   CHAR1

   MOVLW   '0'
   BTFSC   CHAR2,6
   MOVLW   'A'-10
   SUBWF   CHAR2

   CLRC               ;Carry's always set here, so clear it.
   RLF     CHAR1,W    ;W = MSB * 2
   SWAPF   CHAR1      ;CHAR1 = MSB * 16
   RRF     CHAR1      ;CHAR1 = MSB * 8
   ADDWF   CHAR1,W    ;W = (MSB * 8) + (MSB * 2) = MSB * 10
   ADDWF   CHAR2,W    ;W = MSB * 10 + LSB

> In either case, you can remove the BTFSC's and the MOVLW's following
> them since the BTFSC will always skip the MOVLW. Bit 6 of your digit's
> ASCII will always be clear for decimal digits '0' through '9'.

   True, but it'll be set for hex digits 'A' through 'F'.

   If the two input bytes are hex digits in the range 0-F, you need
the
   BTFSC/MOVLWs; if they're only in the range 0-9, you don't.

   -Andy

=== Andrew Warren -- KILLspamaiwKILLspamspamcypress.com
=== Principal Design Engineer
=== Cypress Semiconductor Corporation
===
=== Opinions expressed above do not
=== necessarily represent those of
=== Cypress Semiconductor Corporation

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email RemoveMElistservTakeThisOuTspammitvma.mit.edu with SET PICList DIGEST in the body


2002\02\04@181423 by Bob Barr

flavicon
face
On Mon, 4 Feb 2002 14:29:55 -0800, Andrew Warren wrote:

>Bob Barr wrote:

<snip>

{Quote hidden}

Andy,

The input digits do have to be in the ASCII '0'-'9' range (otherwise
doing the '* 10' wouldn't be right.)

Royce's original input data is a pair of decimal characters to be
converted to hex. The characters 'A' through 'F' just can't be part of
valid input data.


Royce,
Do you need your result in BCD form (for displaying the two digits) or
in its true hex form (for indexing a table or some such)?



Regards, Bob

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email spamBeGonelistservspamBeGonespammitvma.mit.edu with SET PICList DIGEST in the body


2002\02\04@200139 by Andrew Warren

flavicon
face
Bob Barr <TakeThisOuTPICLISTEraseMEspamspam_OUTmitvma.mit.edu> wrote:

> The input digits do have to be in the ASCII '0'-'9' range (otherwise
> doing the '* 10' wouldn't be right.)
>
> Royce's original input data is a pair of decimal characters to be
> converted to hex. The characters 'A' through 'F' just can't be part of
> valid input data.

   See?  THIS is what happens when you take a perfectly good
software
   guy and make him work on everything BUT software for a year and a
   half.  Sigh...

   Ok, my code and I were both confused.  Here's the routine to
   translate two ASCII hex digits (0-9 or A-F) into a hex value.  I
   don't remember Royce saying that his two ASCII digits were
decimal,
   so this might be what he wanted:

       LIST R = DEC

       MOVLW   '0'
       BTFSC   CHAR1,6
       MOVLW   'A'-10
       SUBWF   CHAR1

       MOVLW   '0'
       BTFSC   CHAR2,6
       MOVLW   'A'-10
       SUBWF   CHAR2

       SWAPF   CHAR1,W
       ADDWF   CHAR2,W

   (By the way... On piclist.com, there's a code fragment attributed
to
   me which purports to perform the above translation.  It contains
two
   extra lines between the SWAPF and the final ADDWF; I have no idea
how
   those lines got there, and can only assume that the code was
taken
   out of context.)

   Here's the code to translate two ASCII decimal digits (0-9) into
a
   hex value.  If Royce's input characters ARE decimal, this is the
   routine he wants:

       LIST R = DEC

       MOVLW   '0'
       SUBWF   CHAR1
       SUBWF   CHAR2

       CLRC               ;Carry's always set here, so clear it.
       RLF     CHAR1,W    ;W = MSB * 2
       SWAPF   CHAR1      ;CHAR1 = MSB * 16
       RRF     CHAR1      ;CHAR1 = MSB * 8
       ADDWF   CHAR1,W    ;W = (MSB * 8) + (MSB * 2) = MSB * 10
       ADDWF   CHAR2,W    ;W = MSB * 10 + LSB

   -Andy

=== Andrew Warren -- RemoveMEaiwspamTakeThisOuTcypress.com
=== Principal Design Engineer
=== Cypress Semiconductor Corporation

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservEraseMEspam.....mitvma.mit.edu with SET PICList DIGEST in the body


2002\02\04@203518 by Royce Simmons

picon face
Thanks All,

I really appreciate all the answers.  The code below is what I was looking
for.  I had worked
on this this morning and I had almost the code below but I did not use the
neat trick of setting
up a default subtraction value before testing the input character.  I am
learning slowly but
well,  I think.  I had 2 hex characters (0 to F) for input and I needed the
get the result in the W reg
as a hex number, ex: IN = A1 (ASCII),  output = A1 (HEX).

Works great,  Royce

{Original Message removed}

2002\02\04@212544 by Bob Barr

flavicon
face
On Mon, 4 Feb 2002 20:32:53 -0500, Royce Simmons <EraseMEw2rbnspamPRODIGY.NET>
wrote:

>Thanks All,
>
>I really appreciate all the answers.  The code below is what I was looking
>for.  I had worked
>on this this morning and I had almost the code below but I did not use the
>neat trick of setting
>up a default subtraction value before testing the input character.  I am
>learning slowly but
>well,  I think.  I had 2 hex characters (0 to F) for input and I needed the
>get the result in the W reg
>as a hex number, ex: IN = A1 (ASCII),  output = A1 (HEX).
>

Sorry, my mistake. I thought you had two decimal digits as your input.

Regards, Bob

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email RemoveMElistservEraseMEspamEraseMEmitvma.mit.edu with SET PICList DIGEST in the body


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