Shop OBEX P1 Docs P2 Docs Learn Events
ASCII to Hex — Parallax Forums

ASCII to Hex

askpolyaskpoly Posts: 11
edited 2010-06-07 23:53 in General Discussion
I need to accept a two byte input from Hyperterminal via Uart object. This input will be ASCII. I then need to convert this input to hex for output on a shift register. From what I can tell, there is not a single method for doing this. I am considering bringing in the ASCII character, then using some sort of array as a look up table. Is there a simpler method? Inputs will range from 0x00 to 0xFF (the user will not need to input the "0x" of course.

Thanks
George

Comments

  • Beau SchwabeBeau Schwabe Posts: 6,545
    edited 2010-06-07 23:53
    George,

    If you treat each incoming byte as a separate character and apply the pseudo code below, then after the two bytes, the result in 'H' should be what you are after....
    '---------------------------  This code gets applied to each successive incoming byte --------------
    H = H and $F                      ' <- just look at the LSN (Least Significant Nibble)
    H = H *   $10                     ' <- Shift the value LEFT by 16 (move LOW nibble to HIGH nibble) 
    If N>47 and N<58 H = H + (N - 48) ' <- ASCII check for 0 to 9 
    If N>64 and N<71 H = H + (N - 55) ' <- ASCII check for A to F
    '---------------------------------------------------------------------------------------------------
    
    



    Where:
    N is the ASCII value
    H is the Hex value that you want

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
Sign In or Register to comment.