How to add or subtract the value of string?

For general Flowcode discussion that does not belong in the other sections.
Post Reply
samtin
Posts: 28
http://meble-kuchenne.info.pl
Joined: Sat Jul 10, 2021 2:43 pm
Has thanked: 4 times
Been thanked: 2 times

How to add or subtract the value of string?

Post by samtin »

Hello everybody,
I want to add or subtract different values from the total string. How to add or subtract different values ? Please example.
Thanks!
Attachments
string value.fcfx
(6.74 KiB) Downloaded 48 times

mnfisher
Valued Contributor
Posts: 938
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: How to add or subtract the value of string?

Post by mnfisher »

What are you trying to do? Subtract / add a number to a string ("1" + "2" = "3") or subtract / take parts of the string ( "1234" -> "123")

If the first =- the easiest way would be convert it to an integer first then add / subtract and then convert back (using StringtoInt$ then ToString$)

Code: Select all

.string = "123"
.x = StringToInt$(.string) + 123
.string = ToString$(.x)
Martin

samtin
Posts: 28
Joined: Sat Jul 10, 2021 2:43 pm
Has thanked: 4 times
Been thanked: 2 times

Re: How to add or subtract the value of string?

Post by samtin »

Thank you very much for the answer. I think I understand, I'll try to see if it works.

samtin
Posts: 28
Joined: Sat Jul 10, 2021 2:43 pm
Has thanked: 4 times
Been thanked: 2 times

Re: How to add or subtract the value of string?

Post by samtin »

The first variant works very well. Because you aroused my curiosity, I would like to know the second option.
Thank you!

mnfisher
Valued Contributor
Posts: 938
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: How to add or subtract the value of string?

Post by mnfisher »

To get an individual character from a string use [index]

for example

Code: Select all

.str = "123"
.a = .str[0]   // = '1'
.b = .str[1]  // = '2'
.c  = .str[2]  // = '3'
note that there is a termination character (0) - so .str[3] = 0 here. So you can truncate a string by setting a termination character

Code: Select all

.str = "123"
.str[1] = 0   // .str ="1"
You can extract substrings using Left$, Mid$ and Right$

Code: Select all

.str = "1234"
.s1 = Left$(.str, 2) // .s1 = "12"
.s2 = Mid$(.str, 1, 2) // .s2 = "23"
.s3 = Right$(.str, 2) // .s3 = "34" 
An important thing to note is that a string is made up of ASCII encoded characters thus '1' does not equal 1 (note the single quotes around the '1' - denote a character constant rather than an integer.)

For a single character - the conversion to an integer value is easy:

Code: Select all

.n = '1'. // '0'..'9'
.x = .n - '0'  // .x = 1


As a programming challenge - it is possible to write 'string' calculation routines.
For example AddStr("123", "123") -> "246" this can be used to allow the manipulation of any length of number. Addition is easiest to implement (division not quite so easy !) Although this is rather wasteful of memory (1 byte per digit) - the use of BCD (Binary Coded Decimal) which holds two digits per byte is still sometimes used. This also has the advantage that conversion to 'human readable' form is trivial. Floating point numbers are another complication....

Martin

samtin
Posts: 28
Joined: Sat Jul 10, 2021 2:43 pm
Has thanked: 4 times
Been thanked: 2 times

Re: How to add or subtract the value of string?

Post by samtin »

Thank you very much, I will study later and I will test this variant which I think is great.

samtin
Posts: 28
Joined: Sat Jul 10, 2021 2:43 pm
Has thanked: 4 times
Been thanked: 2 times

Re: How to add or subtract the value of string?

Post by samtin »

Hello everybody,
I tried to add an integer "123" to the string "123A5D" and it didn't work. The last three "A5D" characters are ignored. Calculates only the first three characters "123". Can you help me with another way to add or subtract from the string.
Thanks!
Attachments
string value 2ab.fcfx
(7.99 KiB) Downloaded 46 times

mnfisher
Valued Contributor
Posts: 938
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: How to add or subtract the value of string?

Post by mnfisher »

In this case - A5D is not a number (StringToInt$ is for decimal (base 10) numbers)- so conversion stops at this point.. You'll need to treat it as hexadecimal (base 16) instead..

There isn't an inbuilt hex string to int so we'll have to roll our own...

Martin

mnfisher
Valued Contributor
Posts: 938
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: How to add or subtract the value of string?

Post by mnfisher »

Something like:
HexStr.fcfx
(10.42 KiB) Downloaded 57 times
Note there is NumberToHex$ to convert a number to a hex string.

The code only allows uppercase 'A' to 'F' - you might want to allow lowercase too?
Hex numbers 'may' be preceded by 0x - you might want to check for this?

Martin

samtin
Posts: 28
Joined: Sat Jul 10, 2021 2:43 pm
Has thanked: 4 times
Been thanked: 2 times

Re: How to add or subtract the value of string?

Post by samtin »

Thank you very much, Mr. Martin, everything you posted helps me. I will come back if I can't solve it, but I think I will solve it.

Post Reply