format conversion...
fdelement
Posts: 4
Hi all,
I am currently working on a project which requires the BS2 to read in values (serial) from another device.· I got the BS2 to do so correctly, however I am left with a string in which I need to convert, can anyone help?
CODE
' {$STAMP BS2}
' {$PBASIC 2.5}
Pitch VAR Byte(4)··············· 'Make a 4-byte array
Pitch(3)=0························· 'Put 0 in the last byte
SEROUT 2,16416,[noparse][[/noparse]66]·········· 'Sends command to external device
SERIN 1,16416,[noparse][[/noparse]STR pitch\3]· 'Get 3-byte string
DEBUG "Pitch = "
DEBUG STR pitch,CR··········· ·'Display the string
OUTPUT
Pitch = 7A3
So now I have a string called Pitch that has the values 7A3 (HEX).· I need to convert it to DEC by doing the following...
7*(16^2)+10*(16^1)+3 and save it as a decimal number.· The 10 is A in HEX.· How would I approach this using the BS2 editor?
Any suggestions or comments would be really appreciated!!
Thanks
I am currently working on a project which requires the BS2 to read in values (serial) from another device.· I got the BS2 to do so correctly, however I am left with a string in which I need to convert, can anyone help?
CODE
' {$STAMP BS2}
' {$PBASIC 2.5}
Pitch VAR Byte(4)··············· 'Make a 4-byte array
Pitch(3)=0························· 'Put 0 in the last byte
SEROUT 2,16416,[noparse][[/noparse]66]·········· 'Sends command to external device
SERIN 1,16416,[noparse][[/noparse]STR pitch\3]· 'Get 3-byte string
DEBUG "Pitch = "
DEBUG STR pitch,CR··········· ·'Display the string
OUTPUT
Pitch = 7A3
So now I have a string called Pitch that has the values 7A3 (HEX).· I need to convert it to DEC by doing the following...
7*(16^2)+10*(16^1)+3 and save it as a decimal number.· The 10 is A in HEX.· How would I approach this using the BS2 editor?
Any suggestions or comments would be really appreciated!!
Thanks
Comments
DEBUG DEC STR pitch,cr
deno
SERIN 1,16416,[noparse][[/noparse]HEX3 pitch]· 'Get 3-byte hex number
Don't use BYTE as a variable name--it is a reserved symbol.
If you need to convert it in place, then something like this...
X=0
FOR idx=0 TO 2
SELECT myByte(idx)
CASE "0" TO "9"
X=X*16+(myByte-"0")
CASE "A" TO "F"
X=X*16+(myByte-"A"+10)
CASE ELSE 'huh?
DEBUG "ERROR IN INPUT",CR
ENDSELECT
NEXT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
My BS2 will receive a stream of 3 bytes in HEX format. So I thought a good way of reading the three bytes is by using STR. Thus when I run this code I will get the following output...
CODE
' {$STAMP BS2}
' {$PBASIC 2.5}
Pitch VAR Word················
SEROUT 2,16416,[noparse][[/noparse]66]··········
SERIN 1,16416,[noparse][[/noparse]HEX3 pitch]
DEBUG "Pitch = "
DEBUG DEC pitch,CR
OUTPUT
Pitch = 2048
I need to some how be able to save this DEC pitch (2048) decimal number to a variable because I will need to perform some mathematical operation on it later.· How can I do that?
Post Edited (fdelement) : 5/18/2005 5:07:37 AM GMT
IF pitch=2048 THEN DEBUG "got it "
DEBUG HEX pitch, TAB, DEC pitch, TAB, BIN pitch ' prints pitch in different bases
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
in any case, Thanks a whole bunch Tracy, you've totally helped me with this issue!