Page 1 of 1
Problem with ToString$ function
Posted: Mon Jul 15, 2024 2:44 pm
by pilot123
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.
Re: Problem with ToString$ function
Posted: Mon Jul 15, 2024 3:32 pm
by Steve-Matrix
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).
Re: Problem with ToString$ function
Posted: Mon Jul 15, 2024 4:19 pm
by pilot123
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
Re: Problem with ToString$ function
Posted: Mon Jul 15, 2024 5:42 pm
by Steve-Matrix
Oops. Need to use Int2FormattedString to ensure the second part of the string has leading zeros. See attached.
Re: Problem with ToString$ function
Posted: Mon Jul 15, 2024 6:48 pm
by pilot123
Thanks Steve
That works fine I will add your solution into my project.
Thanks.
Re: Problem with ToString$ function
Posted: Tue Jul 16, 2024 10:06 am
by pilot123
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.
Re: Problem with ToString$ function
Posted: Tue Jul 16, 2024 10:51 am
by BenR
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.
Re: Problem with ToString$ function
Posted: Tue Jul 16, 2024 11:46 am
by pilot123
Hi Ben
Thanks for your help, all working fine now.
Re: Problem with ToString$ function
Posted: Wed Jul 17, 2024 9:25 am
by pilot123
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