Possible Duplicate:
Is there a strtol equivalent that does not require a null-terminated string?
strtod with limited string length
I am trying to convert c 'strings' into various c numeric types: int, float, long int... the 'strings' I have are not '\0' terminated chars, but I have lengths for every string, so I have to copy the chars into buffer, add '\0' to the end, then use atoi or strtol...
Are there some lib/functions that already implement functions like the following?
strToInt(char *str, int str_len)
strToFloat(char *str, int str_len)
In computer programming, a null-terminated string is a character string stored as an array containing the characters and terminated with a null character (a character with a value of zero, called NUL in this article).
size_t strlen(const char* s) strlen(s) returns the length of null-terminated string s. The length does not count the null character. For example, strlen("rabbit") = 6.
The end of the string is automatically terminated by a zero value, creating a “null terminated string.” This allows the end of the message to be easily detected by a receiving device.
Solution: Use sprintf() function. You can also write your own function using ASCII values of numbers.
Maybe don't even bother with a library. In the integer case it'd be really easy to do it yourself:
int str2int(const char* str, int len)
{
int i;
int ret = 0;
for(i = 0; i < len; ++i)
{
ret = ret * 10 + (str[i] - '0');
}
return ret;
}
For floats and doubles, you could use the same code for the integral part, and then a loop that decreases the influence of each successive character by a factor of ten for the decimal bit.
Extra work comes in with all the bells and whistles you want to add - negative numbers (I didn't bother with those in the example code, but they're easy enough to add), scientific notation, validity checking, etc. - but it's not that hard to do once you decide to sit down and do it.
Hope that helps!
C strings are by definition char buffers terminated by null. Otherwise those are not C strings.
I would say that the easiest way, if you have a buffer that is not a C string - convert it to string, and use the standard functions (atoi etc.). Usually it will not even require allocation of more memory, but only a copy of the current string.
Since in your usage case it seems like the non-null-terminated string exists as part of a larger string (non-numeric character immediately following the number), you can probably just use strtol
and strtod
as-is. They will automatically stop when they reach the end of the number, and in fact they'll give you a pointer to the next character if you ask them for it.
If the data in the buffer following the number is not further text, or if it might butt into a subsequent numeric character, ask yourself if you can be sure that the data is writable and not being read by any other thread while you're processing it. If so, just backup the following character and replace it with a null character, call strtol
or strtod
, then restore the character you overwrote.
If all else fails, use strndup
(POSIX, or roll your own) to make a copy that's a C string. If you know the length is bounded, you could avoid allocation and just copy it into a local automatic array variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With