Hi
I have noticed a problem with the ToString$ function, I am using an ULong variable, which I am using as a counter. Every Thing works fine until you get to a 10 digit number ,when yo get to a number which is 10 digits in length you no longer get the correct string displayed. Instead you get a minus number which is the difference between the maximum ULong number of 429467295 and the number you are converting.
I have uploaded two programs, one with a 9 digit number which works correctly, and one with a 10 digit number which illustrates the problem.
This problem exists both in Hardware and Simulation.
Is there a solution to this problem?
Many Thanks.
Problem with ToString$ function
-
- Posts: 30
- http://meble-kuchenne.info.pl
- Joined: Fri Feb 26, 2021 1:50 pm
- Has thanked: 1 time
- Been thanked: 2 times
Problem with ToString$ function
- Attachments
-
- ULongNumber_ToString_10 Digits.fcfx
- (11.99 KiB) Downloaded 299 times
-
- ULongNumber_ToString_9 Digits.fcfx
- (11.99 KiB) Downloaded 312 times
-
- Matrix Staff
- Posts: 1465
- Joined: Sat Dec 05, 2020 10:32 am
- Has thanked: 204 times
- Been thanked: 347 times
Re: Problem with ToString$ function
Generally, variable overflow is something to be aware of with embedded projects, and you will get similar issues with any integer datatypes you use. For example, if you have a BYTE data type which has the value 255, then adding 1 to it will make it become zero and not 256. Other integer data types have similar issues.
You could use a floating point variable if your values are very large, but this is usually unnecessary and could affect program speed and size.
The attached shows a way to work around your specific issue. If the number is larger that the largest "Long" type, then creating a string involves a few extra steps.
For the conversion from a string to an int, if the string is 10 characters long then it is split into 2 separate strings. It is assumed the string is all decimals (i.e. no leading or training spaces and no decimal point).
You could use a floating point variable if your values are very large, but this is usually unnecessary and could affect program speed and size.
The attached shows a way to work around your specific issue. If the number is larger that the largest "Long" type, then creating a string involves a few extra steps.
For the conversion from a string to an int, if the string is 10 characters long then it is split into 2 separate strings. It is assumed the string is all decimals (i.e. no leading or training spaces and no decimal point).
- Attachments
-
- ULongNumber_ToString_ST.fcfx
- (15.09 KiB) Downloaded 302 times
Re: Problem with ToString$ function
Hi Steve
Thanks for you quick response, much appreciated. I have tried your updated program, which works but not 100%. If I set the Counter to an initial value of 3000000000 then the String becomes 3 Counter value of 3000000011 String becomes 311, the zeros are missing.
Is there a way to fix this problem.
Many Thanks
Thanks for you quick response, much appreciated. I have tried your updated program, which works but not 100%. If I set the Counter to an initial value of 3000000000 then the String becomes 3 Counter value of 3000000011 String becomes 311, the zeros are missing.
Is there a way to fix this problem.
Many Thanks
-
- Matrix Staff
- Posts: 1465
- Joined: Sat Dec 05, 2020 10:32 am
- Has thanked: 204 times
- Been thanked: 347 times
Re: Problem with ToString$ function
Oops. Need to use Int2FormattedString to ensure the second part of the string has leading zeros. See attached.
- Attachments
-
- ULongNumber_ToString_ST.fcfx
- (15.12 KiB) Downloaded 304 times
Re: Problem with ToString$ function
Hi Steve
I have tried to program a BL0080 board with your updated program, but it will not compile due to error message.
Please see below.
C:\Users\user\Downloads>REM DIR: FCD\PIC\batch\pic_xc8_comp.bat
C:\Users\user\Downloads>"C:\Program Files (x86)\Flowcode\Compilers\picV2\\bin\xc8-cc.exe" -mcpu=16F18877 "ULongNumber_ToString_ST (6).c" -w=359,1273,1388 -fno-short-float -fno-short-double -std=c90 -Os
ULongNumber_ToString_ST (6).c
135: error: (182) illegal conversion between types
void -> pointer to unsigned char
ULongNumber_ToString_ST (6).c
135: error: (181) non-scalar types can't be converted to other types
(908) exit status = 1
Error returned from [xc8.exe]
Hope you can help.
Thanks.
I have tried to program a BL0080 board with your updated program, but it will not compile due to error message.
Please see below.
C:\Users\user\Downloads>REM DIR: FCD\PIC\batch\pic_xc8_comp.bat
C:\Users\user\Downloads>"C:\Program Files (x86)\Flowcode\Compilers\picV2\\bin\xc8-cc.exe" -mcpu=16F18877 "ULongNumber_ToString_ST (6).c" -w=359,1273,1388 -fno-short-float -fno-short-double -std=c90 -Os
ULongNumber_ToString_ST (6).c
void -> pointer to unsigned char
ULongNumber_ToString_ST (6).c
(908) exit status = 1
Error returned from [xc8.exe]
Hope you can help.
Thanks.
-
- Matrix Staff
- Posts: 1926
- Joined: Mon Dec 07, 2020 10:06 am
- Has thanked: 501 times
- Been thanked: 684 times
Re: Problem with ToString$ function
Hello,
This is subtely different and should compile correctly for you now. I just broke one of the calculations up into a few more steps using an intermediate variable.
This is subtely different and should compile correctly for you now. I just broke one of the calculations up into a few more steps using an intermediate variable.
Regards Ben Rowland - MatrixTSL
Flowcode Online Code Viewer (Beta) - Flowcode Product Page - Flowcode Help Wiki - My YouTube Channel
Flowcode Online Code Viewer (Beta) - Flowcode Product Page - Flowcode Help Wiki - My YouTube Channel
Re: Problem with ToString$ function
Hi
I failed to notice yesterday, that when the program was running on my H/W BL0080 Board, the String is not correct.
If Counter = 4200000001
The String = 4000049665
All works fine when simulated, but is failing to work on H/W. Any ideas to why this is happening?
Thanks
I failed to notice yesterday, that when the program was running on my H/W BL0080 Board, the String is not correct.
If Counter = 4200000001
The String = 4000049665
All works fine when simulated, but is failing to work on H/W. Any ideas to why this is happening?
Thanks