Hex to Ascii

Hello group,

I am a virgin in embedded. I have an 68HC11 and want to display a hex value from a memory location on a LCD display.

I have read that the value has to be converted to ascii before it can be displayed. Any attempt so far has caused junk on the display.

Can someone show me a function to convert hex to ascii ?

Thanks, Peter

Reply to
Peter Pohlmann
Loading thread data ...

Thanks for the help. Yes I am writing in assembler. Any assembler examples anywhere ?

Reply to
Peter Pohlmann

"tim" schreef in bericht news:biqqm4$97j$03$ snipped-for-privacy@news.t-online.com...

ASCII '0' is 0x30 or 48.

--
Thanks,
Frank Bemelman
(remove 'x' & .invalid when sending email)
Reply to
Frank Bemelman

First break the hex value down into bytes, then for each byte use:

FirstChar = (hexval>>4) & 15; FirstChar += (FirstChar>9)? ('A'-10) : '0'; SecondChar = hexval & 15; SecondChar += (SecondChar>9)? ('A'-10) : '0';

or, what is probably less code:

static const char HexDigits[] = "0123456789ABCDEF"; FirstChar = HexDigits[ (hexval>>4) & 15 ]; SecondChar = HexDigits[ hexval & 15 ];

-Robert Scott Ypsilanti, Michigan (Reply through newsgroups, not by direct e-mail, as automatic reply address is fake.)

Reply to
Robert Scott

Here's the C code:

char hexd( char digit ) { return "0123456789ABCDEF"[ digit & 0xF ]; }

-- _ Kevin D. Quitt 91387-4454 snipped-for-privacy@Quitt.net 96.37% of all statistics are made up

Reply to
Kevin D. Quitt

oops, I took the first digit from one and the second from the other, (I guess you can see that!)

tim

Reply to
tim

A good place for examples of this sort of thing is the asm file for the Buffalo monitor.

One binary byte = 2 ascii characters (left half, right half) The code segment below does both. First call OUT_RB for the right half, restore the original byte in A, then call OUT_LB for the left half. OUT_ASC is just a subroutine that puts the resulting bytes somewhere.

; Convert A, BIN to ASCII (A is lost)

OUT_LB LSRA ; shift data to right LSRA ; " LSRA ; " LSRA ; " OUT_RB ANDA #0FH ; mask top half ADDA #30H ; \ convert to ascii CMPA #39H ; | BLE OUT_A ; | jump if 0-9 ADDA #07H ; / convert to hex A-F OUT_A JSR OUT_ASC ; output character RTS

Regards Paul Bealing

formatting link

Reply to
Paul Bealing

Since we're talking about 68xx tricks of the trade...

AND A #$0F ; mask off nibble ADD A #0 ; clear half-carry bit in condition code DAA ; result range -> 00 - 09, 10 - 15 ADD A #$F0 ; result range -> F0 - F9 carry clear, 00 - 05 carry set ADC A #$40 ; result range -> 30 - 39 (ASCII 0-9), 41 - 46 (ASCII A-F)

Mark Zenier snipped-for-privacy@eskimo.com Washington State resident

Reply to
Mark Zenier

To be pedantic, there are no hex values. A nibble is a 4 bit binary value that can be _represented_ as a hexidecimal digit.

Reply to
Bryan Hackney

Paul Bealing writes: [snip]

Or convert both with:

OUT2HEX pshA bsr out_LB pulA bra out_RB

OUT_LB LSRA ; shift data to right LSRA ; " LSRA ; " LSRA ; " OUT_RB ANDA #0FH ; mask top half ADDA #30H ; \ convert to ascii CMPA #39H ; | BLE OUT_A ; | jump if 0-9 ADDA #07H ; / convert to hex A-F OUT_A jmp OUT_ASC ; output character

Reply to
Everett M. Greene

Not realy serious:

char s[11]; sprintf(s,"%x",value);

--
42Bastian
Do not email to bastian42@yahoo.com, it's a spam-only account :-)
Use @epost.de instead !
Reply to
42Bastian Schick

or save the branch:

out2hex psha lsra lsra lsra lsra bsr out_nibble1 ; no and needed ! pula out_nibble: anda #0xf out_nibble1: adda #0x30 cmpa #0x39 ble out_a adda #0x7 out_a: jmp out_asc

--
42Bastian
Do not email to bastian42@yahoo.com, it's a spam-only account :-)
Use @epost.de instead !
Reply to
42Bastian Schick

Now, that's a cool piece of code !

--
42Bastian
Do not email to bastian42@yahoo.com, it's a spam-only account :-)
Use @epost.de instead !
Reply to
42Bastian Schick

Anyone in alt.folklore.computers know who came up with this? As I remember, I first saw it in the debug monitor in the Poly 88 (a.k.a. Microaltair). An 8080 based S-100 machine. So the technique was from before 1977.

Mark Zenier snipped-for-privacy@eskimo.com Washington State resident

Reply to
Mark Zenier

Might as well add the 8051 version I always use ...

H2ASC: anl A,#0FH add A,#90H da A addc A,#40H da A

Regards Peter Ihnat Uni of Wollongong

Reply to
Peter Ihnat

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.