Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

64bit integer conversion from string

I'm dealing with the problem of reading a 64bit unsigned integer unsigned long long from a string. My code should work both for GCC 4.3 and Visual Studio 2010.

I read this question and answers on the topic: Read 64 bit integer string from file and thougth that strtoull would make the work just fine and more efficiently than using a std::stringstream. Unfortunately strtoullis not available in Visual Studio's stdlib.h.

So I wrote a short templated function:

template <typename T>
T ToNumber(const std::string& Str)
{
    T Number;
    std::stringstream S(Str);
    S >> Number;
    return Number;
}

unsigned long long N = ToNumber<unsigned long long>("1234567890123456789");

I'm worried about the efficiency of this solution so, is there a better option in this escenario?

like image 467
Antonio Pérez Avatar asked Nov 22 '11 08:11

Antonio Pérez


People also ask

How do you create a 64-bit integer?

You can declare 8-, 16-, 32-, or 64-bit integer variables by using the __intN type specifier, where N is 8, 16, 32, or 64. The types __int8 , __int16 , and __int32 are synonyms for the ANSI types that have the same size, and are useful for writing portable code that behaves identically across multiple platforms.

How much is a 64-bit integer?

A 64-bit signed integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). A 64-bit unsigned integer.

Is Long a 64-bit integer?

Windows: long and int remain 32-bit in length, and special new data types are defined for 64-bit integers.

What is 64bit integer in Java?

The size of an int in Java is completely independent of the 32-bitness or 64-bitness of a JDK. It is always 4 bytes = 32 bits = −2,147,483,648 to 2,147,483,647. If you want a 64-bit integer, use a long , which is always 64 bits = 8 bytes.


1 Answers

See http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/d69a6afe-6558-4913-afb0-616f00805229/

"It's called _strtoui64(), _wcstoui64() and _tcstoui64(). Plus the _l versions for custom locales.
Hans Passant."

By the way, the way to Google things like this is to notice that Google automatically thinks you're wrong (just like newer versions of Visual Studio) and searches for something else instead, so be sure to click on the link to search for what you told it to search for.

like image 143
Cheers and hth. - Alf Avatar answered Sep 21 '22 06:09

Cheers and hth. - Alf