Need help converting to ASCII Hex format


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2003
    Location
    San Diego
    Posts
    35

    Question Need help converting to ASCII Hex format

    This is the task…
    I have a number stored in a 16 bit word. The number is up to 5 digits like 01389.
    I can also convert it to the format $30, $31, $33, $38, $39 in a 4 byte string. If needed. Each digit is 0 to 9 decimal… never any alpha chr’s. Max value for the expected data is 09999

    I am looking to convert the 5-digit number into a 3 byte “ASCII Hex format” so the number above would look like… $00, $13, $89 in 3 hex bytes. (Looks like the decimal number when looking at the 3 byte HEX code.)

    The first byte is not used in this application so it can be hard coded to $00
    I need some help on how to do this.

    Thanks,
    Tcbcats

  2. #2
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    44


    Did you find this post helpful? Yes | No

    Default

    Where is your data stored ?
    Variable or EEprom or something else?
    Last edited by Robson; - 25th August 2007 at 20:08.

  3. #3
    Join Date
    Mar 2004
    Location
    UK-Midlands
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    Hi tcbcats,

    This is just off the top of my head and will need written and debugged.

    I can also convert it to the format $30, $31, $33, $38, $39 in a 4 byte string. If needed. Each digit is 0 to 9 decimal… never any alpha chr’s. Max value for the expected data is 09999
    Subtract each number with $30 giving 00, 01, 03, 08 and 09 Then

    For example to get the first LSB = (08*16)+09 this should give $89
    Do the same for the other numbers.

    Please this is off the top of my head and I havn't checked it out! It may be of some help?

    Bob

  4. #4
    Join Date
    Dec 2003
    Location
    San Diego
    Posts
    35


    Did you find this post helpful? Yes | No

    Default Stored in a variable

    Quote Originally Posted by Robson View Post
    Where is your data stored ?
    Variable or EEprom or something else?
    It is in a variable... not in EEprom or from a serial source.
    Tcbcats

  5. #5
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    44


    Did you find this post helpful? Yes | No

    Default

    Try this
    Code:
    B0      VAR BYTE
    B1      VAR BYTE
    B2      VAR BYTE
    B3      VAR BYTE
    BWord1  Var BYTE
    BWord2  VAR BYTE
    BWord   VAR WORD
    VarWord VAR WORD
    
    VarWord = 1389 ' Here is your number you defined
    
    B3 = VarWord DIG 3
    B2 = VarWord DIG 2
    B1 = VarWord DIG 1
    B0 = VarWord DIG 0 
    
    BWord1 = B3 * 16 + B2
    BWord2 = B1 * 16 + B0
    
    BWord = BWord1 * $100 + BWord2 ' Here stands your Word like 1389 as hex

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Lightbulb

    Code:
    Value     VAR WORD
    Buf       VAR BYTE[3]
    DigLoop   VAR BYTE
    
    Value = 01389
    
    For DigLoop = 5 to 1 STEP -2
        Buf((6-DigLoop)/2) = ((Value DIG DigLoop) << 4) + Value DIG (DigLoop -1)
    Next DigLoop 
    
    LCDOUT HEX2 Buf(0),":",HEX2 Buf(1),":",HEX2 Buf(2)
    Displays 00:13:89
    <br>
    DT

  7. #7
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    44


    Did you find this post helpful? Yes | No

    Default

    Darrel something is not going in my head.
    Code:
    Value = 01389 ' <- Is the "0" really included ?

  8. #8
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    The compiler will ignore any leading 0's.
    That's the way Tcbcats showed it, so I left it in there.
    <br>
    DT

  9. #9
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    44


    Did you find this post helpful? Yes | No

    Default

    @Darrel
    i mean this line.

    Code:
    For DigLoop = 5 to 1 STEP -2
        Buf((6-DigLoop)/2) ' = Buf((6-5)/2) = 0.5 ??? Does it really works? Round it to zero?
    Next DigLoop
    I don´t tried it out, maybe it works

  10. #10
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Sure it works.

    But PBP uses integer math. So there are no decimals.

    With STEP -2 the loop will execute 3 times, with the values 5, 3, 1 in DigLoop.
    Since it's Integer math, (6-DigLoop)/2 will return 0, 1 and 2 respectively.

    Code:
    loop    Buf((6-DigLoop)/2) = ((Value DIG DigLoop) << 4) + Value DIG (DigLoop -1)
    5       BUF(      0      ) = (       $00              ) +        0  = $00
    3       BUF(      1      ) = (       $10              ) +        3  = $13
    1       BUF(      2      ) = (       $80              ) +        9  = $89
    hth,
    DT

Similar Threads

  1. Hex converting
    By Andre_Pretorius in forum General
    Replies: 9
    Last Post: - 26th June 2009, 02:48
  2. hex ascii help please
    By ffr58kk90 in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 29th December 2006, 21:09
  3. Converting ASCII to HEX
    By BobP in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 27th September 2006, 10:21
  4. ascii characters to hex
    By Peter1960 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 26th January 2006, 04:06
  5. Converting Bytes from hex to Dec
    By Rob Martin in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 8th April 2005, 19:44

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts