Hello everybody,
I want to add or subtract different values from the total string. How to add or subtract different values ? Please example.
Thanks!
How to add or subtract the value of string?
-
- Posts: 47
- http://meble-kuchenne.info.pl
- Joined: Sat Jul 10, 2021 2:43 pm
- Has thanked: 12 times
- Been thanked: 6 times
How to add or subtract the value of string?
- Attachments
-
- string value.fcfx
- (6.74 KiB) Downloaded 132 times
-
- Valued Contributor
- Posts: 1462
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 136 times
- Been thanked: 713 times
Re: How to add or subtract the value of string?
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$)
Martin
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)
-
- Valued Contributor
- Posts: 1462
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 136 times
- Been thanked: 713 times
Re: How to add or subtract the value of string?
To get an individual character from a string use [index]
for example
note that there is a termination character (0) - so .str[3] = 0 here. So you can truncate a string by setting a termination character
You can extract substrings using Left$, Mid$ and Right$
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:
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
for example
Code: Select all
.str = "123"
.a = .str[0] // = '1'
.b = .str[1] // = '2'
.c = .str[2] // = '3'
Code: Select all
.str = "123"
.str[1] = 0 // .str ="1"
Code: Select all
.str = "1234"
.s1 = Left$(.str, 2) // .s1 = "12"
.s2 = Mid$(.str, 1, 2) // .s2 = "23"
.s3 = Right$(.str, 2) // .s3 = "34"
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
Re: How to add or subtract the value of string?
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!
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 133 times
-
- Valued Contributor
- Posts: 1462
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 136 times
- Been thanked: 713 times
Re: How to add or subtract the value of string?
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
There isn't an inbuilt hex string to int so we'll have to roll our own...
Martin
-
- Valued Contributor
- Posts: 1462
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 136 times
- Been thanked: 713 times
Re: How to add or subtract the value of string?
Something like:
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
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