Bytes to string

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
Walbeek
Flowcode v5 User
Posts: 68
Joined: Thu Mar 01, 2007 10:48 am
Location: Netherlands
Been thanked: 3 times
Contact:

Bytes to string

Post by Walbeek »

Hi there,

I have some (basic) questions about flowcode FC6.

I collect several characters A to Z and they are stored in bytes as 0xHH.
Is it possible to put these bytes together as a string?

Is it also possible to do this the other way around?
So, when a string is received, split to several bytes?

When a string variable is 20 characters long , can I change the length in the controller?

Please let me know.
Greetings, Rinie
Flowcode V7 user

User avatar
SteveM
Posts: 55
Joined: Tue Mar 25, 2014 2:09 pm
Has thanked: 28 times
Been thanked: 65 times
Contact:

Re: Bytes to string

Post by SteveM »

Hi,
Yes. You can do all those things. In reality, a String is just an Array of Bytes with a few special properties. Just like an Array, you can read and write individual characters using the [brackets], and FC is not fussy whether you use text or bytes to set the values. For example...

Code: Select all

  my_string = "xxxxx"
  my_string[0] = 'A'       (my_string is now "Axxxx")
  my_string[3] = 66        (my_string is now "AxxBx"; 66 is the code for 'B')
  my_byte = my_string[2]   (my_byte is now 120; the code for 'x')
You can also safely compare bytes with characters within decision making icons - so "IF my_string[0] == 65 THEN ..." and "IF my_string[0] == 'A' THEN ..." both mean exactly the same thing.

The only difference to a normal array is when dealing with the length. The default size of 20 just reserves memory on the chip for that many characters. If the string grows longer, more memory is automatically assigned (assuming there is any memory free!). When you define a new String in the "Create a new variable" dialogue, you can specify a different memory reserve by putting the size in brackets after the name - e.g. my_string[100].

More often, the string is shorter than the memory reserve. This is handled by "null terminating" the string - the last character is always a hidden NULL (zero byte). You can't print that character, it's just a marker for the string ending. So if you are reading through a string one letter at a time, you know to end the loop if you find a zero. For example, the string "ABCDE" is equivalent to a Byte array containing [65, 66, 67, 68, 68, 0]. Any characters found in memory AFTER the zero terminator should be considered "garbage" left over from a previous run of the program. And note, if you are reserving your own string memory, that the NULL counts as one byte of memory!

Walbeek
Flowcode v5 User
Posts: 68
Joined: Thu Mar 01, 2007 10:48 am
Location: Netherlands
Been thanked: 3 times
Contact:

Re: Bytes to string

Post by Walbeek »

Hello Steve,

Thanks for your reply.
I have try to put the string together with:
name = character[0] + character[1] + character[2] + ..... + character[15]

This doesn't work, the calculation below does.
name = naam_begin + name_end

On my display I need a room-name and the temperature.
The room-name can be 13 characters, than a space, than 21,3 and than degrees C (2 characters).
When the room-name is less then 13 characters, the degrees can move to the left.
For this I would want to change the length of the string.

stringname[length] also doesn't work as a variable.
Greetings, Rinie
Flowcode V7 user

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:

Re: Bytes to string

Post by Benj »

Hello,
stringname[length] also doesn't work as a variable.
Length$(stringname) will give you the length of the string variable.
name = character[0] + character[1] + character[2] + ..... + character[15]
This might be better off being split up into multiple lines.

name = character[0] + character[1]
name = name + character[2]
name = name + character[3]

Or you can do it in a loop.

idx = 0
While idx < 16
{
name[idx] = character[idx]
idx = idx + 1
}
name[idx] = 0

The 0 we add to the end of the string signifies the end of the string so when we do things like print string only the valid data in the variable is shown.

User avatar
SteveM
Posts: 55
Joined: Tue Mar 25, 2014 2:09 pm
Has thanked: 28 times
Been thanked: 65 times
Contact:

Re: Bytes to string

Post by SteveM »

SteveM wrote: If the string grows longer, more memory is automatically assigned (assuming there is any memory free!)
As programmers often like to say "your milage may vary!". As kersing helpfully reminded me (thanks!), when working with microcontrollers that have limited amounts of memory, this kind of automatic re-sizing is unlikely to happen. So, if you know you are going to need long string, it's advisable to reserve enough memory in advance when creating a new string variable.
Reserve_string_memory.PNG
(20.77 KiB) Downloaded 4411 times

Brendan
Posts: 243
Joined: Tue Nov 27, 2012 12:53 pm
Location: Cambridge, UK
Has thanked: 140 times
Been thanked: 118 times
Contact:

Re: Bytes to string

Post by Brendan »

Further to Ben's advice, I can add that I've been caught out before by trying to do 'too much' on one line when concatenating strings, or combining strings with functions, for example...

String2 = String1 + " = " + ToString(Integer_A) + ToString(Integer_B)

I've had very quirky results, even with plenty of available RAM and low occupancy.

As a result, I now never concatenate more than two components of a string on a single calculation line, and have avoided all such issues by doing so. Using the above example...

String2 = String1 + " = "
String2 = String2 + ToString(Integer_A)
String2 = String2 + ToString(Integer_B)

Due to some issues in the past with strings concatenated in component macros (I vaguely recall the LCD component being one such example), I now always calculate the entire string first and assign the result to a single string variable (such as String2 in the above example), then declare the resultant variable in the component macro.


All the best,
Brendan

Post Reply