View Full Version : Hex converting
Andre_Pretorius
- 16th June 2009, 19:36
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
Andre_Pretorius
- 16th June 2009, 20:30
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
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
Melanie
- 17th June 2009, 08:29
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...
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...
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
Andre_Pretorius
- 17th June 2009, 15:11
Thanks
This is how I hot it working, you were right about the A being 65
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
Ioannis
- 18th June 2009, 08:29
Hmm, some people still like spaggeti much more!
Hope at least there is a good salsa with it...
Ioannis
ukemigrant
- 21st June 2009, 17:02
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.
' 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/.
Andre_Pretorius
- 25th June 2009, 17:42
Thank you tried the new code and works like a charm
aratti
- 26th June 2009, 00:15
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.
ukemigrant
- 26th June 2009, 00:38
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.
ukemigrant
- 26th June 2009, 02:48
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.
' 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...
Powered by vBulletin® Version 4.1.7 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.