Convert hex to dec
Robban
Posts: 124
Hi!
How do i convert hex to dec?
like
"1A F8" -> 6904
Regards
Robert
How do i convert hex to dec?
like
"1A F8" -> 6904
Regards
Robert
Comments
my command for the lcd is
serin 1,16468,[noparse][[/noparse]skip6,str hexstr\5]
.
.
.
serout 2,84,[noparse][[/noparse] hexstr(i)]
Is there some reason why you're not using the HEX formatter for processing your input text? If your format is indeed "HHbHH" where "HH" are hex digits and "b" is a blank, you could use this input statement to get the value into the word variable x:
To display as a decimal number on the lcd, you'd use:
here is my program
hexstr var byte (6)
i var byte
main:
serout 0,16468,[noparse][[/noparse]"010C",13]
serin 1,16468,[noparse][[/noparse]skip 6,hex2 hexstr.highbyte,skip 1,hex2 hexstr.lowbyte]
for i =1 to 4
serout 2,84,[noparse][[/noparse]dec hexstr]
next
serout 2,84[noparse][[/noparse]"?n"]
change row on display
pause 1000
goto main
but i get Hightbyte marked and "Expected a smaller-siza variable modifier
*confused*
i wrote instead
hexstr var word
main:
serout 0,16468,[noparse][[/noparse]"010C",13]
serin 1,16468,[noparse][[/noparse]skip 6,hex2 hexstr.highbyte,skip 1,hex2 hexstr.lowbyte]
serout 2,84,[noparse][[/noparse]dec hexstr]
serout 2,84[noparse][[/noparse]"?n"]
change row on display
pause 1000
goto main
When i connect the stamp to my laptop with this program it works perfectly using a teminalprogram like Hyperterminal or Pbasic:s term.
However i intend to connect the stamp to my ELM327 see link for more info
http://www.elmelectronics.com/DSheets/ELM327DS.pdf
And when i plug it in i dont get anything.
so i modified the program and came up with this
hexstr var word
main:
serout 0,16468,[noparse][[/noparse]"010C",13]
serin 1,16468,[noparse][[/noparse]skip 6,hexstr.highbyte,hexstr.lowbyte]
serout 2,84,[noparse][[/noparse]hexstr.highbyte,hexstr.lowbyte]
serout 2,84[noparse][[/noparse]"?n"]
change row on display
pause 1000
goto main
and using this program with the elm i only get the "1A"
if i then change the SKIP 6 to SKIP 10 then i only get the "F8"
I also think you could use "SERIN 1,16468,[noparse][[/noparse]hex junk, hex junk, hex hexstr.highbyte, hex hexstr.lowbyte]". The OSD chip is providing a sequence of hex values always separated by blanks (and ended with a return probably). The hex formatter skips any blanks, then processes any number of hex digits up to the next delimiter (blank or return) which is exactly what you need. You won't need the initial skip formatter because there really are two hex numbers in front of the ones you want and the "hex junk" will process them and they'll be ignored. junk needs to be some unneeded byte variable.
i hade to add on the serin....
serin tx,baud,[noparse][[/noparse]skip 6,hexstr.highbyte,hexstr.lowbyte,skip1, hexstr2.highbyte,hexstr2.lowbyte]
so now i got 2 varibles with hex..
is there some kind of formular to multiply/devide etc to get the 6904?
if so How??
Post Edited (Robban) : 3/6/2007 9:40:06 PM GMT
You do the same thing with lowbyte substituted for highbyte, then do:
Then you do the whole thing the same for hexstr2, then do:
Your result is in hexstr.
·
and i got just as you said...6904 on the laptop and lcd....
but when i got down to the car nothing happens....
i also made a program that displays both 1a and f8 (with my computers) and when i got down to the car...all i got was 1a and f...
*soon getting a HEX headache*
Note that "value" must be declared as a word and "hexstr" as a 5 byte array.
But i wonder one thing...
If tyou remember i used the
hexstr var byte
serout tx,baud,[noparse][[/noparse]"010c",13]
serin rx,baud,[noparse][[/noparse]skip 6 str hexstr/5]
for i =1 to 6
debug hexstr(i)
next
end
and when i used this and plugged it in to the elm i got my 1A F8 on the screen
Now i wonder...is there any way that i can convert this to 6904 despite it is "Var byte"
here it is...
hexstr VAR Byte(5)
hexa VAR Word
i VAR Byte
value VAR Word
main:
SEROUT 0,16468,[noparse][[/noparse]"010c",13]
SERIN 1,16468,[noparse][[/noparse]SKIP 6, STR hexstr\5]
FOR i=0 TO 1
IF hexstr(i)>="0" AND HEXstr(i) <="9" THEN a
IF hexstr(i)>="a" AND hexstr(i)<="f" THEN b
ENDif
a:
hexstr(i)=hexstr(i)-"0"
b:
hexstr(i)=hexstr(i)-"A"+10
NEXT
FOR i=3 TO 4
IF hexstr(i)>="0" AND HEXstr(i) <="9" THEN c
IF hexstr(i)>="a" AND hexstr(i)<="f" THEN d
c:
hexstr(i)=hexstr(i)-"0"
d:
hexstr(i)=hexstr(i)-"A"+10
NEXT
value=hexstr(0)<< 12 + hexstr(1) << 8 +hexstr(3) << 4 + hexstr(4)
PAUSE 1000
DEBUG DEC value
SEROUT 2,84,[noparse][[/noparse]DEC value,"?n"]
GOTO main
SEROUT 2,84,[noparse][[/noparse]"?n"]
PAUSE 100
GOTO main
when i send 1a f8 i get 45249, not the 6904,why?
2) That won't fix your problem though. When you modified what I wrote, you changed the logic so it won't work. You didn't say why you changed it and I assume you either have an old version of the PBasic editor or you didn't specify the newer version of the syntax in a compiler directive and the editor probably complained about the THEN/ENDIF form of the IF statement. Here's a version with the "old" syntax:
Post Edited (Mike Green) : 3/8/2007 4:07:32 PM GMT
You need to have a version of this for each loop (with different labels).
It's not at all helpful for you to tell me "but no luck". It gives me no useful information. I would strongly suggest that you use DEBUG statements to examine what's happening as your program goes along. When I get stuck, I sometimes put a DEBUG statement after every assignment statement and every input statement so I can see what's actually happening to the variables in my program. I usually use the DEC formatter so I can see the actual numeric value (if there's a character I'm storing in the variable and it's not printable). Don't just copy what I give you. Figure out how it works. Simulate your program on a piece of paper with a column for each variable and each line represents another point in time as your program executes. Figure out what the IF statements do. Figure out what the FOR statements do, step by step if needed.
Where down in the car and tried it and all i got was a constant number.
I begin to understand your lines...:-D
I just hooked up the stamp between my computer ,using one computer with pbasic term to send my hex...and the aother to show me the hex for each (i) and a debug dec value
and this is what i got
1: 1
1: A
2: F
2: 8
4931
Shouldn´t the value be 6904?
when i was down in the car....this was a constant value of 4931.
Post Edited (Robban) : 3/8/2007 6:58:41 PM GMT
If x contains a character, the first IF statement checks whether the character is a digit. If so, it subtracts the value of the character "0" to give a number between 0 and 9. If the character is outside of the range, nothing changes. The second IF statement checks to see if the character is an upper case letter between "A" and "F". If so, it subtracts the value of the character "A" and adds ten to give a number between 10 and 15. This is the way to translate a single hex digit to an equivalent numeric value. You can add another IF statement to take care of the lower case letter situation.
In your case, I simply repeated this logic 4 times, once for each hex digit, then combined the 4 "nibbles" to make a 16 bit numeric value which you can display using a DEC formatter with a SEROUT statement.
I calculated it and it´s missing 1632...
Post Edited (Robban) : 3/8/2007 7:51:33 PM GMT
Unsoundcode is absolutely right (I keep confusing PBasic and Spin sometimes). It should be:
' {$STAMP BS2}
k VAR Byte
hexstr VAR Byte(5)
hexa VAR Word
i VAR Byte
value VAR Word
main:
SEROUT 0,16468,[noparse][[/noparse]"010c",13]
SERIN 1,16468,[noparse][[/noparse]STR hexstr\11]
FOR i=6 TO 11
SEROUT 2,84,[noparse][[/noparse]hexstr(i)] - display the hex on lcd
NEXT
FOR k = 6 TO 7
IF hexstr(i) < "0" OR hexstr(i) > "9" THEN skip1x
hexstr(i) = hexstr(i) - "0"
skip1x:
IF hexstr(i) < "a" OR hexstr(i) > "f" THEN skip2x
hexstr(i) = hexstr(i) - "A" + 10
skip2x:
NEXT
FOR k = 9 TO 10
IF hexstr(i) < "0" OR hexstr(i) > "9" THEN skip3x
hexstr(i) = hexstr(i) - "0"
skip3x:
IF hexstr(i) < "a" OR hexstr(i) > "f" THEN skip4x
hexstr(i) = hexstr(i) - "A" + 10
skip4x:
NEXT
value =(hexstr(6) << 12) + (hexstr(7) << 8) + (hexstr(9) << 4) + hexstr(10)
DEBUG "hex1:",hexstr(0),CR
DEBUG "hex2:",hexstr(1),CR
DEBUG "hex3:",hexstr(3),CR
DEBUG "hex4:",hexstr(4),CR
DEBUG "hex5:",hexstr(6),CR
DEBUG "hex6:",hexstr(7),CR
DEBUG "hex7:",hexstr(9),CR
DEBUG "hex8:",hexstr(10),CR
DEBUG DEC value
SEROUT 2,84,[noparse][[/noparse]"?n",DEC value] - display the value on lcd
PAUSE 1000
'GOTO main
Post Edited (Robban) : 3/8/2007 8:55:47 PM GMT
1) You're now treating hexstr as an 11 character string, but it's only declared as a 5 character string. You have to either make hexstr bigger or go back to the way it was (as a 5 character string).
2) You never fixed the IF statements. They're testing for lower case letters, but the assignment that follows is converting them as if they were upper case. I noted my error earlier and said you should fix it. I also suggested adding another IF/assignment/label for the upper case letters and you haven't done it yet. Please, if I make a suggestion, please, either protest the change and give me your reasons (which may be perfectly correct), or make the changes as I've given them. Otherwise, I will think you're doing one thing, but you've done another, and I can't understand why my suggestion doesn't work.