Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get an unsigned long out of a string?

What's the safest and best way to retrieve an unsigned long from a string in C++?

I know of a number of possible methods.

First, converting a signed long taken from atol.

char *myStr; // Initalized to some value somehow.
unsigned long n = ((unsigned)atol(myStr));

The obvious problem with this is, what happens when the value stored in myStr is larger than a signed long can contain? What does atol retrieve?

The next possibility is to use strtoul.

char *myStr; // Initalized to some value somehow.
unsigned long n = strtoul(myStr, 0, 10);

However, this is a little over complicated for my needs. I'd like a simple function, string in, unsigned long base 10 out. Also, the error handling leaves much to be desired.

The final possibility I have found is to use sscanf.

char *myStr; // Initalized to some value somehow.
unsigned long n = 0;
if(sscanf(myStr, "%lu", n) != 1) {
    //do some error handling
}

Again, error handling leaves much to be desired, and a little more complicated than I'd like.

The remaining obvious option is to write my own either a wrapper around one of the previous possibilities or some thing which cycles through the string and manually converts each digit until it reaches ULONG_MAX.

My question is, what are the other options that my google-fu has failed to find? Any thing in the C++ std library that will cleanly convert a string to an unsigned long and throw exceptions on failure?

My apologies if this is a dupe, but I couldn't find any questions that exactly matched mine.

like image 591
Daniel Bingham Avatar asked Sep 27 '09 18:09

Daniel Bingham


People also ask

Which library function can convert an unsigned long to a string?

2) Which library function can change an unsigned long integer to a string? The correct option is (c). Explanation: The function ultoa() is used for converting an unsigned long integer to a string.

What is an unsigned long?

Unsigned long is a numeric field type that represents an unsigned 64-bit integer with a minimum value of 0 and a maximum value of 264-1 (from 0 to 18446744073709551615 inclusive).

How do you initialize an unsigned long long?

First, be sure your compiler supports the long long type. Second, add a "ULL" suffix to the number. Just to clarify, unsigned long long has to be at least 64-bits. @GMan, unsigned long long doesn't have to be anything until C++0x officially becomes ISO C++ 2011.


3 Answers

You can use strtoul with no problem. The function returns an unsigned long. If convertion can not be performed the function return 0. If the correct long value is out of range the function return ULONG_MAX and the errno global variable is set to ERANGE.

like image 105
Patrice Bernassola Avatar answered Sep 21 '22 07:09

Patrice Bernassola


One way to do it:

stringstream(str) >> ulongVariable;
like image 25
mmx Avatar answered Sep 24 '22 07:09

mmx


template <class T>
T strToNum(const std::string &inputString,
           std::ios_base &(*f)(std::ios_base&) = std::dec)
{
    T t;
    std::istringstream stringStream(inputString);

    if ((stringStream >> f >> t).fail())
    {
        throw runtime_error("Invalid conversion");
    }
    return t;
}


// Example usage
unsigned long ulongValue = strToNum<unsigned long>(strValue);
int intValue             = strToNum<int>(strValue);

int intValueFromHex      = strToNum<int>(strHexValue,std::hex);
unsigned long ulOctValue = strToNum<unsigned long>(strOctVal, std::oct);
like image 20
RC. Avatar answered Sep 24 '22 07:09

RC.