Page 1 of 1

How to add or subtract the value of string?

Posted: Sun Jun 26, 2022 2:23 pm
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!

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

Posted: Sun Jun 26, 2022 2:44 pm
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

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

Posted: Sun Jun 26, 2022 3:21 pm
by samtin
Thank you very much for the answer. I think I understand, I'll try to see if it works.

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

Posted: Sun Jun 26, 2022 3:39 pm
by samtin
The first variant works very well. Because you aroused my curiosity, I would like to know the second option.
Thank you!

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

Posted: Sun Jun 26, 2022 5:31 pm
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

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

Posted: Sun Jun 26, 2022 9:13 pm
by samtin
Thank you very much, I will study later and I will test this variant which I think is great.

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

Posted: Mon Jun 27, 2022 3:36 pm
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!

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

Posted: Mon Jun 27, 2022 4:51 pm
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

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

Posted: Mon Jun 27, 2022 6:43 pm
by mnfisher
Something like:
HexStr.fcfx
(10.42 KiB) Downloaded 66 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

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

Posted: Mon Jun 27, 2022 7:11 pm
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.