Strings and arrays

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply

Please rate this article out of 5: (1 being the worst and 5 being the best)

No votes
No votes
Total votes: 8

Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am

Strings and arrays

Post by Sean »


Flowcode strings and arrays

In addition to the numeric Byte, Integer, and Floating-point variables, Flowcode is also capable of handling Strings. String variables and their associated functions simplify the manipulation and use of text data.

A string is simply a continuous block of memory locations that can be referenced using a single name. Each memory location in the string is equivalent to a Byte variable and is capable of storing a single character.

If a variable is defined as a String in the Flowcode variable manager when it is created, Flowcode reserves a block of memory locations (20 by default) and keeps a record of the first location. The number of bytes reserved for the string can be altered using the 'Array Size...' button in the Variable Manager.

Flowcode provides a String Manipulation block that allows access to a range of string related functions. These functions are distinct from the arithmetic functions available via the Calculation block.

String functions
Strings are defined using characters within double-quotes. A string variable Str1 can be loaded with a string using the following command in a String manipulation block:

Str1 = "Hello"

Strings can also be added together:

Str2 = "world!"

Str3 = Str1 + " " + Str2

Str3 now contains the string "Hello world!"

Note: The string versions of the + and = operations are not equivalent to the arithmetic operations using the same symbols.

The Left$(), Right$(), and Mid$() functions allow sections of an existing string to be isolated and tested or copied.

Length$() returns the number of characters in a string to a numeric variable.

Char$() allows a single character to be copied from a string into a Byte variable

ToUpper$() and ToLower$() allow the cases of all the text characters in a string to be set.

Compare$() allows two strings to be compared. The cases of the text characters can be set to be significant or not significant.

ToString$(), FloatToString$(), and NumberToHex$() allow numeric variable values to be converted to strings of various formats.

StringToInt$() and StringToFloat$() allow strings containing numeric characters to be converted to their equivalent numeric values.

The Flowcode help files provide a fuller description of each function.

Note: Some of these string functions are new to Flowcode V4

Strings can be displayed directly on an LCD using the PrintString macro in the Flowcode LCD component. The USB, ADC and RS232 components are now also capable of generating, receiving or sending strings.

ASCII codes
Each character in a string is stored in one byte of the string memory using its ASCII value.
The ASCII standard allocates numeric values to all the printable, and some non-printable/control, characters.

Numeric characters '0' ~ '9' have the ASCII code values 48 ~ 57
Upper case alphabetic characters 'A' ~ 'Z' have the ASCII code values 65 ~ 90
Lower case alphabetic characters 'a' ~ 'z' have the ASCII code values 97 ~ 122

Other codes are used to represent punctuation/format characters and control codes.

Full ASCII code tables are readily available in many documents and on the internet.

An individual character is converted to its ASCII code value by placing it within single-quotes.

A Byte variable Char1 can be assigned the ASCII value of 'X' (88) using the following command:

Char1 = 'X'

The following line would produce an error as X is not a numeric value:

Char1 = X

With a numeric character, both versions would be legal, but would produce different values:

Char1 = 7
Char1 = '7'

The first line would load Char1 with the numeric value 7, but the second line would load char1 with the ASCII code value for the character 7 - which is 55.

A string StrX = "Hello123" is stored in memory as the individual ASCII character codes:

'H' = 72 <- StrX[0]
'e' = 101
'l' = 108
'l' = 108
'o' = 111
'1' = 49
'2' = 50
'3' = 51
\n = 0

The string variable contains a reference to the location of the first character 'H'.
The \n character is a control code (NULL) with the numeric value 0. This is used as the terminating character of a string and its location is managed by Flowcode when the string is manipulated by any of the String functions.

A special case in Flowcode allows the \n character to be overwritten if the string length is exactly equal to the size of the array.

The string can be accessed from a Calculation block as an array. Locations within the string can be accessed individually using the variable's name and an index value.

StrX[0] = 'H'
StrX[1] = 'e'
StrX[6] = '2'

The index value [x] can be a variable, allowing the array to be scanned under program control.

When used as an array, the individual locations in StrX do not need to be treated as ASCII characters and can be used to store indexed collections of any Byte value data (0 ~ 255).

There are many applications for collections of data values that can be stored under a single name and individually accessed using a numeric index (conversion tables, data logs etc.).

It is possible to manipulate the individual characters of a string by temporarily treating it as an array and performing arithmetic operations of the character values. Some problems might occur if the result contains non-printable ASCII code values or the NULL terminating character is not managed correctly.

Example program
(12.78 KiB) Downloaded 1971 times
The example program linked to this article demonstrates a range of operations on string/array variables which are displayed on an LCD. The program can be run using Flowcode V3 and Flowcode V4

Post Reply