Monday, February 27, 2012

NXT Blocks - Conversion Utils - Text to Int (GSAText2Int)

Now that we have means of manipulating strings (splitting, concatenating, etc.) I started to think on ways of expanding the usage of strings. One usage is for storage and transport of other type of data. So next come to mind is to build ways to serialize and un-serialize different kind of data.

We already have means to serialize numbers within the NXT software: the Number To Text Building Block. So I decided the inverse of this Block is needed: Text To Int (GSAText2Int).

Practically this Block is capable to convert a a string containing a number in decimal notation to an integer, given the string and the starting offset of the number. The end of the number is marked by the first non numeric character.

1. Input String
2. Offset
3. Default (in case conversion fails this number will be presented 
at the output)

1. Output Value
2. New Offset (this is the point where our number ends)

Input String = "12345", Offset = 0 => Number = 12345, New Offset = 5
Input String = "12345.678", Offset = 0 => Number = 12345, New Offset =5
Input String = "12345.678", Offset = 3 => Number = 45, New Offset = 5

Notice this Block converts integers so the '.' (decimal point) is treated as non numeric.

You can download GSAText2Int here.

No comments:

Post a Comment