Shop OBEX P1 Docs P2 Docs Learn Events
String to Integer and String to Float coversion in Basic stamp. — Parallax Forums

String to Integer and String to Float coversion in Basic stamp.

labheshrlabheshr Posts: 10
edited 2008-04-25 04:22 in BASIC Stamp
Hello Everyone:

I am working on a Bluetooth controlled Boe-Bot and need to accept strings (made using C++ program on PDA) via bluetooth. However after doing this I need to convert them to Float and Integer values and perform certain Boe-Bot actions using these values.

Example: I accept a using SERIN 0, 84,[noparse][[/noparse]certain variable and its extracted bytes (I dont know how to do this either)] on the Bluetooth port (EB500).
a
After doing this , I need to covert the string into integer values- if I received "120" , it should be converted to 120 in integer, since certain arithimatic calculation will be done on this. Similiarly when I receive "0.88" , it should become equivalent 0.88 in float.

Basically I am looking for an equivalent functionality of "atoi" and "atof" in Basic Stamp language.

Any help on this would be greatly appreciated!

Thanks;
Labhesh.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-04-24 23:03
    First of all, there is no floating point.· The Stamps are integer-only and the integers are 16 bits (word) or 8 bits (byte), sometimes 4 bits (nibble) or a single bit.· The method for converting strings of digits to a numeric value is called a formatter and these can appear as prefixes in I/O statements like SERIN and SEROUT.· There are formatters for decimal (DEC), hexadecimal (HEX), and binary (BIN), both variable length and fixed length.· See the Parallax Stamp Basic Manual in the chapters on the SERIN and SEROUT statements and one of the appendices for further information.

    It is possible to do scaled fixed point arithmetic where, for example, 0.88 is represented as 88 and 1.34 is represented as 134, but you'll have to code it yourself.· The compiler won't do it for you.
  • labheshrlabheshr Posts: 10
    edited 2008-04-25 03:48
    Thanks Mike!

    So basically, if I am sending string values to the Boe-Bot, the code to accept them and process them as integers would be of following kind:

    
    szCom     VAR Byte(5)
    
    SERIN 0, 84,[noparse][[/noparse]DEC szCom\5\CR]  
    
    
    



    The data coming in from Bluetooth is actually a text string. Because i have put DEC, it will be accepted by the compiler as a series of digits stored in Byte array szCmom.
    Is this correct?

    Also, then I can do operation on this data such as addition or comparison? For example: if ( szCom = angle) ... do something. IN this case angle is a decimal number.

    Please confirm if this would work for me?

    Otherwise I will have to convert the angle decimal to String format, which I do not know how to do?

    Thank you;
    Labhesh.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-04-25 04:04
    Well, actually no. Please read the manual. It goes into quite a lot of detail. To accept a variable length integer (string of digits) delimited by a non-digit, you'd write "SERIN 0,84,[noparse][[/noparse]DEC szCom]" where szCom is defined as a Word. The conversion is done "on the fly". You don't read in the characters, then apply an operator.

    Once the SERIN is done, the integer value is in szCom and you can do whatever you want numerically with it.
  • labheshrlabheshr Posts: 10
    edited 2008-04-25 04:22
    Thank you again Mike!

    That was great help!

    -Labhesh.
Sign In or Register to comment.