1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

Hex to Ascii

Discussion in 'Embedded' started by Peter Pohlmann, Aug 30, 2003.

  1. 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
     
    Peter Pohlmann, Aug 30, 2003
    #1
    1. Advertisements

  2. Thanks for the help.
    Yes I am writing in assembler.
    Any assembler examples anywhere ?
     
    Peter Pohlmann, Aug 30, 2003
    #2
    1. Advertisements

  3. ASCII '0' is 0x30 or 48.
     
    Frank Bemelman, Aug 30, 2003
    #3
  4. Peter Pohlmann

    Robert Scott Guest

    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.)
     
    Robert Scott, Aug 30, 2003
    #4
  5. Here's the C code:

    char hexd( char digit )
    {
    return "0123456789ABCDEF"[ digit & 0xF ];
    }
     
    Kevin D. Quitt, Aug 30, 2003
    #5
  6. Peter Pohlmann

    tim Guest

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

    tim
     
    tim, Aug 30, 2003
    #6
  7. Peter Pohlmann

    Paul Bealing Guest

    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
    www.pmb.co.nz
     
    Paul Bealing, Aug 31, 2003
    #7
  8. Peter Pohlmann

    Mark Zenier Guest

    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 Washington State resident
     
    Mark Zenier, Aug 31, 2003
    #8
  9. To be pedantic, there are no hex values. A nibble is a 4 bit binary
    value that can be _represented_ as a hexidecimal digit.
     
    Bryan Hackney, Aug 31, 2003
    #9
  10. 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
     
    Everett M. Greene, Sep 1, 2003
    #10
  11. Not realy serious:

    char s[11];
    sprintf(s,"%x",value);
     
    42Bastian Schick, Sep 1, 2003
    #11
  12. 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 Schick, Sep 1, 2003
    #12
  13. Now, that's a cool piece of code !
     
    42Bastian Schick, Sep 2, 2003
    #13
  14. Peter Pohlmann

    Mark Zenier Guest

    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 Washington State resident
     
    Mark Zenier, Sep 3, 2003
    #14
  15. Peter Pohlmann

    Peter Ihnat Guest

    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
     
    Peter Ihnat, Sep 5, 2003
    #15
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.