I have a short value and after performing some bit operations i use sprintf to print it to a character array. Is there any easy way to remove leading zeros.
To huskers:
It seems the easiest way is: where is the source of zeroes? May be you can suppress its generation from the beginning?
If not, write a function to eat leading zeroes in ASCIIZ strings (the standard way in the spirit of good old C...
Second assignment forgets any memory allocations, after that you can use only one first byte of literal string "". sprint()'ing to such pointer will destroy any data. Should be
str[0]=0;
2.
> sprintf(str,"%s%d",str,value);
Using of same string as source and destination is dangerous, it would produce infinite loop if the format string would be for instance " %s%%d".
3.
> the str1 contains 0000418.23
such a result could be received only in case, when on entering to sprintf() str would contain "0000418." - source of zeroes is somewhere earlier.
4. Format specifier %6d has nothing to do with leading zeroes - its purpose is to add spaces in front of the number.
sprintf(str,"%07d",3); results in "0000003"
sprintf(str,"%7d",3); results in " 3"
sprintf(str,"%d",3); results in "3"
But Huskers isn't using a width format specifier in his sprintf statement. It looks like He's starting with an empty string:
""
then basically concatenating the next value using sprintf:
(I'm still not sure where the decimal is coming from, but that's irrelevant)
The result is a numeric string with leading 0's that need to be stripped off.
Consider Eliminating the source of the leading 0's as ArkM suggested or something like I suggested earlier. There are, of course other ways to do the same thing.
Also, mingis pointed out some problems with the code.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.