View Full Version : variable casting/conversion in pBasic

bryan costanich
01-20-2007, 04:20 PM
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?


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, [numberOfBlinks] ' receive one byte

'DEBUG ? numberOfBlinks

SELECT numberOfBlinks
CASE "10"
Temp = 10
CASE "20"
Temp = 20
CASE "30"
Temp = 30
CASE "100"
Temp = 100

there's got to be a better way.

bryan costanich
01-20-2007, 04:53 PM
ok, i found the formatters or whatever, but i can't seem to get the syntax right. i've tried:

temp = DEC(numberOfBlinks)
temp = DEC numberOfBlinks
DEC temp = numberOfBlinks
DEC(temp) = numberOfBlinks

i've also tried

SERIN 16, BAUD_9600, DEC([numberOfBlinks])
SERIN 16, BAUD_9600, DEC [numberOfBlinks]


all of the syntax examples in the book are using


or whatever. how do i do it on a variable?

bryan costanich
01-20-2007, 05:08 PM
ok, i figured it out, but now i have a follow-up question. the syntax is as follows:
SERIN 16, BAUD_9600, [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 [DEC20 numberOfBlinks], but what if it's 100? then i would have to use [DEC3 numberOfBlinks[

so isn't there some smarter formatting available? any suggestions?

Bruce Bates
01-20-2007, 06:56 PM
Bryan -

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:


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

end quote


Bruce Bates

Post Edited (Bruce Bates) : 1/20/2007 1:13:19 PM GMT

01-21-2007, 12:44 AM
Hi bryan, Serin will accept your integer without naming·a character length if you send an end of string or new line character from your C# app. I dont know the syntax in C# but in VB it would be like this

Stamp :- SERIN 16,BAUD_9600,[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.

bryan costanich
01-21-2007, 03:59 AM
ah, i gotcha, newline. cool.

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?

Kevin Wood
01-21-2007, 04:24 AM
If you were awaiting a response from the BS2 immediately following your C# write, you would need to account for the echo to avoid reading it as the response.

bryan costanich
01-21-2007, 04:52 AM
ah, right. but that's the same for any thing you send down the pipe, no? be it a new line character or other

01-21-2007, 05:46 AM
Hi bryan, if your only transmitting from the PC it isn't an issue but if you have two way communication the echo can get mixed with the Stamp to PC data unless you remove it, pins 0 to 15 don't give you that problem. I made the assumption you were transmitting and recieving and that is why I mentioned it, but it looks like you have all that under control.

Jeff T.

bryan costanich
01-21-2007, 03:38 PM
Gotcha. thanks for the headsup.

by the way, the newline works like a charm.