String Handling Questions
Moderator: Benj
-
- 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
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:
Any ideas? (and yes, Extended CPU is disabled.
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:
Any ideas? (and yes, Extended CPU is disabled.
Programmer: PICKIT3
OS: Windows 10 home 64 bit
FlowCode 6.1.2.0 (11.05.2015)
OS: Windows 10 home 64 bit
FlowCode 6.1.2.0 (11.05.2015)
-
- 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
Hi bvolken,
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: Martin
Code: Select all
Stringholder2 = Left$ (Stringholder2,3) + "," + Right$ (Stringholder2,3)
If the number of digits will vary, then a more complex arrangement is required.
For example: Martin
Martin
-
- 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
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.
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)
OS: Windows 10 home 64 bit
FlowCode 6.1.2.0 (11.05.2015)
-
- 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
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.bvolken wrote:Loop thru the string until you find EOS (0x00)
“Integrity is doing the right thing, even when no one is watching.”
― C.S. Lewis
― C.S. Lewis
-
- 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
I am not a fan of the flowcode documentation, but I did read this:
http://www.matrixtsl.com/wiki/index.php ... able_Types
Now . . . Which is correct?
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.
Now . . . Which is correct?
Programmer: PICKIT3
OS: Windows 10 home 64 bit
FlowCode 6.1.2.0 (11.05.2015)
OS: Windows 10 home 64 bit
FlowCode 6.1.2.0 (11.05.2015)
- 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
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.
Or at least ensure all your sting buffers are large enough to contain the maximum expected string length plus 1.
-
- 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
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"?
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)
OS: Windows 10 home 64 bit
FlowCode 6.1.2.0 (11.05.2015)
-
- 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
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: 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.
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.bvolken wrote: So, to keep this from wandering off topic any further: "is a string null terminated or is it not"?
“Integrity is doing the right thing, even when no one is watching.”
― C.S. Lewis
― C.S. Lewis
-
- 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
Gotta be honest, not sure how to even process that bit of information.The documentation is not incorrect but allows different interpretations.
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)
OS: Windows 10 home 64 bit
FlowCode 6.1.2.0 (11.05.2015)
-
- 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
Then don't try. Just accept that different people read different meanings into the same sentence.bvolken wrote:Gotta be honest, not sure how to even process that bit of information.The documentation is not incorrect but allows different interpretations.
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: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)
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?bvolken wrote:Leaving little (but oh so critical) information out of the documentation makes me like it even less.
“Integrity is doing the right thing, even when no one is watching.”
― C.S. Lewis
― C.S. Lewis
- 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
@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.
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.