Convert string to integer (or byte)

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
bert
Posts: 23
Joined: Wed Feb 28, 2007 11:58 pm
Location: The Netherlands

Convert string to integer (or byte)

Post by bert »

I have a string which contains "6" and I want to convert that string to an integer (or byte) so I can use it in a calculation. Which command do I need to use?

Bert

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

Hello Bert

Simply get the byte from the string you wish to use. This can be done by indexing the array eg string[3] will get the 4th character in variable string. There are also functions in the string manipulation that will get you a single character.

Then simply minus 48 or '0' from the number.

eg

number = string[3]
number = number - '0'

bert
Posts: 23
Joined: Wed Feb 28, 2007 11:58 pm
Location: The Netherlands

Post by bert »

Great, thanks Ben!

I now do this to get two numbers out of the string (for example "64"):

number = ( string[0] - 48 ) * 10
number = number + string[1] - 48

Is this the preferred way or is there a smarter command?

Bert

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

Well if you want to be really clever about it you can create a loop that scans ahead and sees if the next character in the string is a number (eg between 48 '0' and 57 '9' ). If it is then the current number gets multiplied by 10 and then the next number is added to create a running total of possible numeric characters (max 6).

However if you are only expecting numbers with a certain amount of digits then your approach is perfect.

Post Reply