Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert strings specified by length (not NUL-terminated) to int/float? [duplicate]

Tags:

c

string

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)
like image 225
Mickey Shine Avatar asked Mar 09 '12 08:03

Mickey Shine


People also ask

Is string null terminated?

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).

How do you find the length of a null-terminated string?

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.

How is string terminated in memory?

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.

How can a number be converted to a string in C?

Solution: Use sprintf() function. You can also write your own function using ASCII values of numbers.


3 Answers

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!

like image 87
Xavier Holt Avatar answered Oct 02 '22 13:10

Xavier Holt


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.

like image 23
MByD Avatar answered Oct 02 '22 13:10

MByD


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.

like image 43
R.. GitHub STOP HELPING ICE Avatar answered Oct 02 '22 14:10

R.. GitHub STOP HELPING ICE