Hex converting


Closed Thread
Results 1 to 10 of 10

Thread: Hex converting

  1. #1

    Default Hex converting

    i have seted up a modbus link with a exsternal device the problem is now that i get my data in as 4 bytes of hex numbers
    eg. 07D9 is what i am getting it must become a year number 2009

    any idea's how i can get this conversion
    thanks in advance

  2. #2


    Did you find this post helpful? Yes | No

    Default

    this is what i have tried so far

    DataA=DATAIN(8)
    Gosub HEXConvert

    The idea is data from the attay is pased to a temp value, a subrotine is called end if the values are bigger than 9 the coresponding digital value is replaced into the temp value, if none of the if statements are true the value are returnd

    it still return the same hex caractor - any ideas


    Code:
    HEXConvert:
    IF DATAA = $A THEN
    	DATAA = 10
    	RETURN
    ENDIF
    IF DATAA = $B THEN
    	DATAA = 11
    	RETURN
    ENDIF
    IF DATAA = $C THEN
    	DATAA = 12
    	RETURN
    ENDIF
    IF DATAA = $D THEN
    	DATAA = 13
    	RETURN
    ENDIF
    IF DATAA = $E THEN
    	DATAA = 14
    	RETURN
    ENDIF
    IF DATAA = $F THEN
    	DATAA = 15
    	RETURN
    ENDIF
    
    RETURN

  3. #3
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    If you make an assumption firstly that you have valid (UPPERCASE) ASCII HEX and not Klingon or some other alien data... then your ASCII HEX to Numeric conversion could be as simple as...

    Code:
    	DataA=AsciiHexByte-48
    	If DataA>9 then DataA=DataA-7
    If you need to handle UPPERCASE or lowercase (valid) HEX, then your code needs an extra line...

    Code:
    	DataA=HEXByteInput-48
    	If DataA>9 then DataA=DataA-7
    	If DataA>15 then DataA=DataA-32
    What YOU are doing is NOT converting ASCII HEX... you are converting one form of Number representation to another form of exactly the same number... $A=10 anyway, so what you are doing is stating...

    If DataA=$A then DataA=10

    ... which is the same as...

    If DataA=10 then DataA=10

    ... which is kinda pointless... what you should have said is...

    If DataA="A" then DataA=10

    ... or to be long-winded about it...

    If DataA=65 then DataA=10

    ... because $A=10 whilst "A"=65

  4. #4


    Did you find this post helpful? Yes | No

    Talking

    Thanks

    This is how I hot it working, you were right about the A being 65
    Code:
    HEXConvert:
    IF DATAA = 65 THEN
    	DATAA = 10
    	RETURN
    ENDIF
    IF DATAA = 66 THEN
    	DATAA = 11
    	RETURN
    ENDIF
    IF DATAA = 67 THEN
    	DATAA = 12
    	RETURN
    ENDIF
    IF DATAA = 68 THEN
    	DATAA = 13
    	RETURN
    ENDIF
    IF DATAA = 69 THEN
    	DATAA = 14
    	RETURN
    ENDIF
    IF DATAA = 70 THEN
    	DATAA = 15
    	RETURN
    else
    	DATAA = DATAA - 48
    ENDIF
    
    RETURN

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,115


    Did you find this post helpful? Yes | No

    Default

    Hmm, some people still like spaggeti much more!

    Hope at least there is a good salsa with it...

    Ioannis

  6. #6
    Join Date
    Apr 2009
    Location
    Pittsburgh, PA
    Posts
    17


    Did you find this post helpful? Yes | No

    Default Better code

    If you're sure your data really just consists of the digits and letters A through F, use this instead. Including comments will help when you come back to it later too.

    Code:
    ' This works because 0 - 9 have decimal codes 48 - 57 and A - F have 
    ' decimal codes 65 - 70 in ASCII
    HEXConvert:
    IF DATAA >= 65 THEN
    	DATAA = DATAA - 55 ' 65 - 55 = 10, 66 - 55 = 11 etc
    else
    	DATAA = DATAA - 48 ' 48 - 48 = 0, 49 - 48 = 2 etc
    ENDIF
    
    RETURN
    http://www.asciitable.com/.

  7. #7


    Did you find this post helpful? Yes | No

    Default

    Thank you tried the new code and works like a charm

  8. #8
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    eg. 07D9 is what i am getting it must become a year number 2009
    An algoritm to convert Hex to Dec is as follows:

    Hex 07D9 = 9x16^0 + 13x16^1 + 7x16^2 + 0x16^3 = Dec 2009

    I don't see how you achieve the convertion with your system!

    Al.
    Last edited by aratti; - 26th June 2009 at 00:18.
    All progress began with an idea

  9. #9
    Join Date
    Apr 2009
    Location
    Pittsburgh, PA
    Posts
    17


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by aratti View Post
    An algoritm to convert Hex to Dec is as follows:

    Hex 07D9 = 9x16^0 + 13x16^1 + 7x16^2 + 0x16^3 = Dec 2009

    I don't see how you achieve the convertion with your system!

    Al.
    I'm sure he's using the partial algorithm he gave to get the 13 from the D for example and then plugging it into another equation like yours.

  10. #10
    Join Date
    Apr 2009
    Location
    Pittsburgh, PA
    Posts
    17


    Did you find this post helpful? Yes | No

    Talking

    Quote Originally Posted by ukemigrant View Post
    If you're sure your data really just consists of the digits and letters A through F, use this instead. Including comments will help when you come back to it later too.

    Code:
    ' This works because 0 - 9 have decimal codes 48 - 57 and A - F have 
    ' decimal codes 65 - 70 in ASCII
    HEXConvert:
    IF DATAA >= 65 THEN
    	DATAA = DATAA - 55 ' 65 - 55 = 10, 66 - 55 = 11 etc
    else
    	DATAA = DATAA - 48 ' 48 - 48 = 0, 49 - 48 = 2 etc
    ENDIF
    
    RETURN
    http://www.asciitable.com/.
    Of course 49 - 48 <> 2 . Luckily it was in the comments but still...

Similar Threads

  1. PIC16F877A pwm use for IR transmission
    By mcbeasleyjr in forum General
    Replies: 0
    Last Post: - 11th July 2009, 18:51
  2. Converting Dec word to Hex string
    By tcbcats in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 28th August 2007, 05:44
  3. Need help converting to ASCII Hex format
    By tcbcats in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 26th August 2007, 19:14
  4. Converting ASCII to HEX
    By BobP in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 27th September 2006, 10:21
  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 : 0

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