How to parse an integer in a glib string (gchar *)?
Solution 1
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:
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.
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.)
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()
Solution 2
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.
Jordan
Updated on June 30, 2022Comments
-
Jordan almost 2 years
I have a string which contains (the digits of) an integer value, and I want to obtain this value as an int. I am aware that there are other methods for doing this such as
atoi()
; however, I'd really like to use glib to do this. Does such a parsing/conversion function exist? -
ptomato over 14 yearsThat'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.
-
Matt Joiner over 14 yearshey, he doesn't mention locale requirements, and if locale isn't an issue, the default will be just fine. :)
-
ptomato over 14 yearsThat'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 over 14 yearsExactly. 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 over 14 yearsI'm very sorry if you found me annoying.
-
Matt Joiner over 14 yearsDon'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.