variable casting/conversion in pBasic
bryan costanich
Posts: 29
i have a variable "200" and i need to parse that into an integer 100. how do i cast, convert, or parse that in pBasic? do they offer any commands?
Context:
i have a c# windows application that opens up a serial port connection, writes a number say "30" to the port and then closes the port.
my bs2 listens on that port and sticks that value into a variable, but that variable is text, not an integer. right now i have this temporary/hacky stuff:
SERIN 16, Baud9600, [noparse][[/noparse]numberOfBlinks] ' receive one byte
'DEBUG ? numberOfBlinks
SELECT numberOfBlinks
CASE "10"
Temp = 10
CASE "20"
Temp = 20
CASE "30"
Temp = 30
CASE "100"
Temp = 100
ENDSELECT
there's got to be a better way.
Context:
i have a c# windows application that opens up a serial port connection, writes a number say "30" to the port and then closes the port.
my bs2 listens on that port and sticks that value into a variable, but that variable is text, not an integer. right now i have this temporary/hacky stuff:
SERIN 16, Baud9600, [noparse][[/noparse]numberOfBlinks] ' receive one byte
'DEBUG ? numberOfBlinks
SELECT numberOfBlinks
CASE "10"
Temp = 10
CASE "20"
Temp = 20
CASE "30"
Temp = 30
CASE "100"
Temp = 100
ENDSELECT
there's got to be a better way.
Comments
temp = DEC(numberOfBlinks)
temp = DEC numberOfBlinks
DEC temp = numberOfBlinks
DEC(temp) = numberOfBlinks
i've also tried
SERIN 16, BAUD_9600, DEC([noparse][[/noparse]numberOfBlinks])
SERIN 16, BAUD_9600, DEC [noparse][[/noparse]numberOfBlinks]
etc.
all of the syntax examples in the book are using
DEBUG DEC 7
or whatever. how do i do it on a variable?
SERIN 16, BAUD_9600, [noparse][[/noparse]DECx numberOfBlinks]
where x is the number of digits. this is fine if i know the number of digits before hand, but what if i don't? for example, if i send in 20, then i would use [noparse][[/noparse]DEC20 numberOfBlinks], but what if it's 100? then i would have to use [noparse][[/noparse]DEC3 numberOfBlinks[noparse][[/noparse]
so isn't there some smarter formatting available? any suggestions?
With output formatter DEC x the x represents the MAXIMUM number of digits to be displayed. So, for 20 it would show "020" and for 100, it would show "100". Variables have no internal format other than as a binary number.
If you have the ASCII equivalent of a decimal number and you want to convert the ASCII "number" to a decimal number, here is a technique suggested by "Bullwinkle" in a prior reply:
quote
Just subtract "0" (letter zero or $30 (hex)) from each byte, turning them into their "real" decimal equivalent. Moving from left to right, multiply result by 10 and add the next digit subtracting "0" each time.
Like this:
A VAR Byte
B VAR Byte
C VAR Byte
D VAR Byte
R VAR Word
A = "1"
B = "2"
C = "3"
D = "4" ' for example
R = A - $30
R = R*10 + B - $30
R = R*10 + C - $30
R = R*10 + D - $30
DEBUG "Result is ",DEC R
R will be stored internally and displayed as 1234 decimal.
If the bytes your want converted to decimal are in contigous memory locations you use indexed addressing, like this:
idx VAR Nib
R = 0
FOR idx = 0 TO 3 ' read bytes
R = R*10 + A(idx) - $30
NEXT
end quote
Regards,
Bruce Bates
Post Edited (Bruce Bates) : 1/20/2007 1:13:19 PM GMT
Stamp :- SERIN 16,BAUD_9600,[noparse][[/noparse]DEC numberOfBlinks]
VB :- Serialport1.Write(my_number & Chr(10))· or· Serialport1.Writeline(my_number)
Also be aware that the default port 16 will echo back so any subsequent transmisions will need to have the echo removed before sending the next integer.
Jeff T.
i'm not sure i follow about the echo though. i understand that the chip echos back all commands, but i'm not doing anything with the echo, so why do i have to remove the echo?
Jeff T.
by the way, the newline works like a charm.