std::strtoul, std::strtoull

Header: <cstdlib>

Interprets an unsigned integer value in a byte string pointed to by str.

# Declarations

unsigned long strtoul ( const char* str, char** str_end, int base );
unsigned long long strtoull( const char* str, char** str_end, int base );

(since C++11)

# Parameters

# Return value

Integer value corresponding to the contents of str on success. If the converted value falls out of range of corresponding return type, range error occurs (errno is set to ERANGE) and ULONG_MAX or ULLONG_MAX is returned. If no conversion can be performed, 0 is returned.

# Example

#include <cstdlib>
#include <errno.h>
#include <iostream>
#include <string>
 
int main()
{
    const char* p = "10 200000000000000000000000000000 30 -40 - 42";
    char* end = nullptr;
    std::cout << "Parsing '" << p << "':\n";
    for (unsigned long i = std::strtoul(p, &end, 10);
        p != end;
        i = std::strtoul(p, &end, 10))
    {
        std::cout << "'" << std::string(p, end - p) << "' -> ";
        p = end;
        if (errno == ERANGE)
        {
            errno = 0;
            std::cout << "range error, got ";
        }
        std::cout << i << '\n';
    }
    std::cout << "After the loop p points to '" << p << "'\n";
}

# See also