tags:

views:

637

answers:

2

I have a string that I want to convert to an int. I am aware that there are other methods for doing this such as atoi; however, I'd really like to use a glib function if one exists. Does such a function exist?

Thanks!

+1  A: 

GLib is a general purpose library that provides a common base to develop applications. This does not mean GLib reimplements all the standard C library, but instead abstracts whatever not available (or not consistent) across all the supported platforms.

So, in short, you must use the standard atoi() function: GLib implements only the gdouble and gint64 variants.

ntd
A: 

GLib provides much of the standard C library with safety checks for input, and enhancements where practical.

The function you're looking for is g_ascii_strtoll().

Pedantic addendum

atoi() treats locale the same way as strtol AND g_ascii_strtoll(). A very careful reading of the manpages and Glib documentation will reveal this. Here are some snippets for those that can't RTFM:

atoi()

The atoi() function converts the initial portion of the string pointed to by nptr to int. The behavior is the same as strtol(nptr, (char **) NULL, 10); except that atoi() does not detect errors.

strtol()

In locales other than the "C" locale, other strings may also be accepted. (For example, the thousands separator of the current locale may be supported.)

g_ascii_strtoll()

Converts a string to a gint64 value. This function behaves like the standard strtoll() function does in the C locale. It does this without actually changing the current locale, since that would not be thread-safe.

Changing locale

If this is not sans-locale enough, you can set the locale through environment variables, and/or explicit calls to setlocale()

Matt Joiner
That's actually the wrong answer. g_ascii_strtoll() isn't an enhancement of atoi() or strtoll(), it's a locale-independent version. The g_ascii_strtoll() docs say:"To handle input from the user you should normally use the locale-sensitive system strtoll() function."ntd's answer was correct, just use atoi() to convert a string to an int. It's not a GLib function, but that's okay.
ptomato
hey, he doesn't mention locale requirements, and if locale isn't an issue, the default will be just fine. :)
Matt Joiner
That's the point, you never know when locale will be an issue. I don't know exactly how the function differs from locale to locale, but I can imagine that in, say, the Korean locale it might not convert Arabic numerals at all...
ptomato
well, they still have numbers right? gotta parse them somehow...
Matt Joiner
Exactly. And g_ascii_strtoll() won't do that. My point is that g_ascii_strtoll() isn't a drop-in replacement for strtoll() or atoi() with safety checks or enhancements, it's an entirely different function with a different purpose. So the answer to the OP's question, "is there a GLib function that replaces atoi()," is "no," which was ntd's answer.
ptomato
I've updated my answer to address ptomato's concerns.
Matt Joiner
I'm very sorry if you found me annoying.
ptomato
Don't worry, ptomato. Being pedantic is great virtue for developers. Just gotta avoid being argumentative, annoying, and unproductive, it's a fine line, and a difficult skill to master.
Matt Joiner