We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hello,
I am removing all sprintf() function from my souce code as this function does not allow me to set the proper thread stack size. Does anyone have any function that implements this conversion?
Thanks Andre Moutinho
itoa(num, str, 10);
And what happens if num is 12345678? You really think a function named IntegerToAscii is up to the task?
Or what do you do if the number is 0.000000000000000012345678?
How do you then store the fractional part in an integer? Or make itoa() make a string of it? Or make the number fit in a 20-character array?
Also remember that sprintf() has more than one way to express a floating point value depending on the formatting string.
Here is the deal - if using sprintf(), you must care about the stack needs. If using own function to convert a floating point value, you must care about the size of the target buffer.
How much do you gain by not getting a stack overflow but instead a buffer overrun?
Hello Westermark, thanks for your reply.
Let me explain what I am doing first. My application is a module that has GPS and GPRS communication. I have a can interface and get some vehicle values such as speed, rotation, hodometer. The ftoa() function will be used only for debug of these values. I have checked their range and will be no larger than 2147483647 and not smaller than 0.00001.
Please read the answers bellow:
>And what happens if num is 12345678? You really think >a function named IntegerToAscii is up to the task?
Yes, it converted with success. Why an itoa() function will not convert this number? The ftoa() function returned 12345678.00000. 12345678 < 2147483647 (0x7ffffff)
>Or what do you do if the number is >0.000000000000000012345678?
This will result in 0.00. But this value is out of the range precision I have stabilished. The result was 0.00000. Thats OK once I have stablished 6 digits of precision.
>How do you then store the fractional part in an >integer?
By multiplying for the precision I want. If I want 0.00001 I then multiply it for 100000.
>Or make itoa() make a string of it? >Or make the number fit in a 20-character array?
Limiting the higher value and precision. I have forgotten to limit the higher value. Will do it.
>Also remember that sprintf() has more than one way to >express a floating point value depending on the >formatting string.
Will not need this.
>Here is the deal - if using sprintf(), you must care >about the stack needs. If using own function to >convert a floating point value, you must care about >the size of the target buffer.
Yes, correct. I will narrow the higher value to 2147483647 (0x7FFFFFFF) or 10 digits. The decimal part in constrained to 5 digits. More one for the '.' and an extra 0x0 for final termination. Total used will be 17 characteres and no buffer overrun.
>How much do you gain by not getting a stack overflow >but instead a buffer overrun?
Nothing, but creating such constraints that works 100% in my application I eliminated both problems.
Regards, Andre