String Handling Questions

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

Moderator: Benj

Post Reply
bvolken
Posts: 14
Joined: Mon Nov 30, 2015 1:57 am
Location: California, United States
Has thanked: 6 times
Been thanked: 9 times
Contact:

String Handling Questions

Post by bvolken »

Variable StringHolder = "123456" (it's a real string, not a number).

I check if the string is Length$ greater than 3, then I want to add a comma, so "123456" should become "123,456". Simple. NotSoMuch

I get three different results from three variations of the identical formula:
Prints1.jpg
Prints1.jpg (20.59 KiB) Viewed 7176 times
Prints2.jpg
Prints2.jpg (19.58 KiB) Viewed 7176 times
Prints3.jpg
Prints3.jpg (21.83 KiB) Viewed 7176 times
Any ideas? (and yes, Extended CPU is disabled.
Programmer: PICKIT3
OS: Windows 10 home 64 bit
FlowCode 6.1.2.0 (11.05.2015)

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: String Handling Questions

Post by medelec35 »

Hi bvolken,

Code: Select all

Stringholder2 = Left$ (Stringholder2,3) + "," + Right$ (Stringholder2,3)
Should only work when using a 6 digit number.
If the number of digits will vary, then a more complex arrangement is required.
For example:
String hand1.png
(116.19 KiB) Downloaded 3955 times
Martin
Martin

bvolken
Posts: 14
Joined: Mon Nov 30, 2015 1:57 am
Location: California, United States
Has thanked: 6 times
Been thanked: 9 times
Contact:

Re: String Handling Questions

Post by bvolken »

Hello medelec35,
Thanks for the feedback! An update:
I do realize what I posted only worked with a 6 character string, I was just baffled by the results of string concatenation.
I went with an even simpler solution:

a) Loop 1 - figure where first comma goes:
Loop thru the string until you find EOS (0x00) - this is a simple "MOD" command without all the math files being downloaded and SUCKING UP ROM
When this loop is done, I know where the first comma goes
b) Loop 2, print character-at-a-time, insert commas
Loop thru the string until you find EOS (0x00), print each character and stick in a comma every 3 spaces (except for the first comma)

Two loops, no math (positive integers only so far). Works perfect.
Programmer: PICKIT3
OS: Windows 10 home 64 bit
FlowCode 6.1.2.0 (11.05.2015)

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: String Handling Questions

Post by kersing »

bvolken wrote:Loop thru the string until you find EOS (0x00)
Keep in mind not all strings in Flowcode are zero terminated. If the length of the string exactly matches the size of the buffer no terminating zero is present. It's safer to use Length$ to determine the number of characters and use it to check if you are at the end of the string.
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

bvolken
Posts: 14
Joined: Mon Nov 30, 2015 1:57 am
Location: California, United States
Has thanked: 6 times
Been thanked: 9 times
Contact:

Re: String Handling Questions

Post by bvolken »

I am not a fan of the flowcode documentation, but I did read this:

Code: Select all

One important difference is in Flowcode strings are null-terminated. This means the first zero-value byte that is encountered in the string marks the end of the string.
http://www.matrixtsl.com/wiki/index.php ... able_Types

Now . . . Which is correct?
Programmer: PICKIT3
OS: Windows 10 home 64 bit
FlowCode 6.1.2.0 (11.05.2015)

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: String Handling Questions

Post by LeighM »

The Wiki comment is pointing out the difference between a byte array and a string. In most cases it will be null terminated, but the comment from kersing is true, in that you are best to check for both string end scenarios. (This is mainly due to historic reasons and backwards compatibility with V5)
Or at least ensure all your sting buffers are large enough to contain the maximum expected string length plus 1.

bvolken
Posts: 14
Joined: Mon Nov 30, 2015 1:57 am
Location: California, United States
Has thanked: 6 times
Been thanked: 9 times
Contact:

Re: String Handling Questions

Post by bvolken »

Thank you also!

I started confused about one thing, now I'm confused about two things.

1) OP was on "Why am I getting three different outputs from string concatenation"? For which I'm still confused, but it is no longer relevant.

2) is a STRING null terminated (and the documentation is correct) or it is NOT (and the documentation is in error).
If you are telling me that a string[20] that contains 20 characters has no null termination, then that means the documentation is not accurate.

I am clear that a string is a variation of a BYTE array, but a BYTE array is not a string.

So, to keep this from wandering off topic any further: "is a string null terminated or is it not"?
Programmer: PICKIT3
OS: Windows 10 home 64 bit
FlowCode 6.1.2.0 (11.05.2015)

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: String Handling Questions

Post by kersing »

bvolken wrote: 2) is a STRING null terminated (and the documentation is correct) or it is NOT (and the documentation is in error).
If you are telling me that a string[20] that contains 20 characters has no null termination, then that means the documentation is not accurate.
The documentation is not incorrect but allows different interpretations. What the documentation tries to clarify is that inserting a zero in a string will make Flowcode behave like the string only contains characters up to the zero. Everything beyond the zero is lost when calling macros or returning results.
bvolken wrote: So, to keep this from wandering off topic any further: "is a string null terminated or is it not"?
If a zero is present in a string it is always interpreted as the end of the string. If the string is exactly the size of the allocated buffer no zero will be present.
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

bvolken
Posts: 14
Joined: Mon Nov 30, 2015 1:57 am
Location: California, United States
Has thanked: 6 times
Been thanked: 9 times
Contact:

Re: String Handling Questions

Post by bvolken »

The documentation is not incorrect but allows different interpretations.
Gotta be honest, not sure how to even process that bit of information.
So, it is null terminated unless it's not. (for clarity of anyone reading this, by "zero" you mean "null" (0x00) since 'zero' can be confused with an ASCII zero character (0x30)
Leaving little (but oh so critical) information out of the documentation makes me like it even less.
Programmer: PICKIT3
OS: Windows 10 home 64 bit
FlowCode 6.1.2.0 (11.05.2015)

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: String Handling Questions

Post by kersing »

bvolken wrote:
The documentation is not incorrect but allows different interpretations.
Gotta be honest, not sure how to even process that bit of information.
Then don't try. Just accept that different people read different meanings into the same sentence.
bvolken wrote:So, it is null terminated unless it's not. (for clarity of anyone reading this, by "zero" you mean "null" (0x00) since 'zero' can be confused with an ASCII zero character (0x30)
The explanation for this oddity was provided a couple of messages ago. And yes, I mean 0x00, null might confuse people with an SQL background and also not be familiar to non computer geeks with english as their second/third/whatever language.
bvolken wrote:Leaving little (but oh so critical) information out of the documentation makes me like it even less.
Feel free not to like it, I've seen a lot worse recently (and also better over the years) for far more expensive products so I don't mind it. BTW, have you checked the documentation lately?
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: String Handling Questions

Post by LeighM »

@kersing -
Thanks for the wiki edit!

@bvolken -
As @kersing mentioned, I hope you understand that we try to make the documentation meaningful to people with wide variations in knowledge. This particular complication arises from the fact that Flowcode can be used to target anything from PCs, with effectively limitless memory, to embedded processors with very little RAM. For the latter case every single byte counts, so Flowcode has historically allowed the use of strings that are not null terminated. In our target language of C in particular it is also up to the user to ensure buffers are defined that are large enough to contain the strings. In general, strings are null terminated, and Flowcode similarly terminates any string it creates with a null, however, Flowcode tries to help learners to avoid buffer overflows, and as mentioned previously, will not terminate with the null if the buffer is not large enough.

Post Reply