String Handling Questions

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

Moderator: Benj

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

Postby bvolken » Sun Dec 06, 2015 7:59 am

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 3700 times


Prints2.jpg
Prints2.jpg (19.58 KiB) Viewed 3700 times


Prints3.jpg
Prints3.jpg (21.83 KiB) Viewed 3700 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)

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8542
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2456 times
Been thanked: 3526 times
Contact:

Re: String Handling Questions

Postby medelec35 » Sun Dec 06, 2015 1:16 pm

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
String hand1.png (116.2 KiB) Viewed 3682 times


Martin
These users thanked the author medelec35 for the post:
bvolken (Sun Dec 06, 2015 6:41 pm)
Rating: 5.26%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

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

Postby bvolken » Sun Dec 06, 2015 6:41 pm

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: 1894
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 541 times
Been thanked: 1058 times
Contact:

Re: String Handling Questions

Postby kersing » Sun Dec 06, 2015 7:04 pm

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.
These users thanked the author kersing for the post (total 2):
medelec35 (Sun Dec 06, 2015 7:08 pm) • bvolken (Mon Dec 07, 2015 5:25 am)
Rating: 10.53%
 
“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

Postby bvolken » Mon Dec 07, 2015 5:20 am

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: 1962
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 474 times
Been thanked: 674 times
Contact:

Re: String Handling Questions

Postby LeighM » Mon Dec 07, 2015 10:23 am

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.
These users thanked the author LeighM for the post:
bvolken (Mon Dec 07, 2015 5:19 pm)
Rating: 5.26%
 

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

Postby bvolken » Mon Dec 07, 2015 5:18 pm

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: 1894
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 541 times
Been thanked: 1058 times
Contact:

Re: String Handling Questions

Postby kersing » Mon Dec 07, 2015 6:02 pm

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.
These users thanked the author kersing for the post:
LeighM (Mon Dec 07, 2015 7:39 pm)
Rating: 5.26%
 
“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

Postby bvolken » Mon Dec 07, 2015 11:17 pm

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: 1894
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 541 times
Been thanked: 1058 times
Contact:

Re: String Handling Questions

Postby kersing » Mon Dec 07, 2015 11:41 pm

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?
These users thanked the author kersing for the post (total 3):
LeighM (Tue Dec 08, 2015 9:43 am) • medelec35 (Tue Dec 08, 2015 9:50 am) • bvolken (Wed Dec 09, 2015 5:35 am)
Rating: 15.79%
 
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

User avatar
LeighM
Matrix Staff
Posts: 1962
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 474 times
Been thanked: 674 times
Contact:

Re: String Handling Questions

Postby LeighM » Tue Dec 08, 2015 9:43 am

@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.
These users thanked the author LeighM for the post:
bvolken (Wed Dec 09, 2015 5:35 am)
Rating: 5.26%