[opensource] uint64_t Question

Evan Powers powers.161 at osu.edu
Mon May 21 09:20:01 EDT 2007


On 5/21/07, Alexander Moore <moorea at cse.ohio-state.edu> wrote:
> Does anybody know if there is an 'atoi' for uint64_t ?  I know there is
> a strtol which will convert a string to a long int (32 bits), but is
> there a function that will convert a string to a long long int
> (uint64_t, 64 bits) ?

I would avoid atoi()/strtol() and variants thereof, because there's no
way for them to signal a conversion error. I also wouldn't use
iostream; it's just too painful.

What you want is sscanf(). From the scanf(3) man page:

    int sscanf(const char *str, const char *format, ...);
    ...
    The q specifier is the 4.4BSD notation for long long, while ll or
the usage of L in
    integer conversions is the GNU notation.

So, you'd do something like
    uint64_t x;
    if (sscanf(s, "%Lu", &x) < 1)
        error();
assuming you're using GCC. If that doesn't work, try "q" instead of
"L". If neither work, try the other options proposed in this thread.

It's even type safe in GCC if you have warnings enabled, which you
really really should. (-Wall).

- Evan

P.S. Personally, I know and love both C *and* C++. I also avoid
programming in *either* whenever possible. I miss C++'s superior
ability to organize code when writing C, and I miss C's elegant
simplicity when writing C++. Python has been picking up the slack for
a while now. It's a good language to learn, if you aren't familiar
with it.


More information about the Opensource mailing list