How to convert string to unsigned long in C++? [duplicate]

。_饼干妹妹 提交于 2020-03-03 13:06:34

问题


I need help in programming. The program has to accept a string that will eventually be turned to unsigned long. But here's the catch, there must be an error catcher that when you enter combination of hex and symbols like a!!!!!! will produce an error and the unsigned long variable must be able to accept and store the input greater that 4294967295 which is FFFFFFFF. I've tried this code segment:

char buffer[256];
unsigned long holder;
fgets(buffer,256,stdin);
holder = strtoul (buffer,NULL,16);

My problem is that when I enter FFFFFFFFF (9 F's) instead of FFFFFFFF (8 F's), the holder will STILL accept 4294967295 even though its more than the. Another thing is that when I combine both hex and symbols like a!!!!!, the fgets still consider the hex A.

Can you please give me an idea on how to do this? If you know any other idea besides this code, please do let me know. Thanks!


回答1:


If you are using the "old C stule strings", then you can add extra checks to see that "all characters were taken" by passing a char * to the strtoul call.

In other words:

 char *end_ptr = NULL;
 ....
 errno = 0;
 holder = strtoul(buffer, &end_ptr, 16);

The end_ptr will point at the character one past the accepted input, so if you enter "a!!!!!", it will point at a '!'.

 if (end_ptr != '\0')  // Should point at "end of string marker". 
 {
     ... do something to indicate error. 
 }

To detect overflow, you will have to rely on errno:

if (errno != 0)
{
    ... deal with errors here . 
}

Obviously, you can do:

if (errno != 0 || *end_ptr != '\0')
{
    .... deal with errors. 
}

Using the C++ std:stoul() function will throw an exception, so something like:

try { holder = std::stoul(buffer); } catch(...) { ... deal with error ... }

would be the C++ style solution.




回答2:


So if you look at this document for strtoul you will see this under the Return Value section:

If the converted value falls out of range of corresponding return type, range error occurs and ULONG_MAX or ULLONG_MAX is returned.

So for out of range check you need code similar to this:

if ( ( holder == ULONG_MAX || holder == ULLONG_MAX ) && errno == ERANGE)

For the a!!!! case looking back at the same document, you will see:

The functions sets the pointer pointed to by str_end to point to the character past the last character interpreted. If str_end is NULL, it is ignored.

you are currently passing in NULL but if you pass in an argument:

char *p;
holder = strtoul (buffer,&p,16);

you can now check whether if *p is a NULL terminator and if so then you processed all the characters otherwise you know you had an issue.

You also have the option of using stoul which throw the following exceptions std::invalid_argument if no conversion could be performed and std::out_of_range if the converted value would fall out of the range of the result type.

For example you could do as follows:

std::string str1(n) ;
size_t pos ;

try
{
   holder = std::stoul( str1, &pos, 16 ) ;
}
catch( std::invalid_argument e )
{
    std::cout << "Invalid argument" << std::endl ;
}
catch ( std::out_of_range  e )
{
    std::cout << "Out of range" << std::endl ;
}

pos will be the index of the last character processed, in your case if pos != str1.length() then it could not process the whole string and your have a problem.




回答3:


As other posters have said - use stoul if you have it.

If you don't, you might be able to do something like:

std::istringstream strm( buffer );

unsigned long holder = 1;
strm >> std::hex >> holder;

if( strm.fail() )
         // out-of-range

if( ! strm.eof() )
         // didn't read all available characters - catches "A!!!"


来源:https://stackoverflow.com/questions/17813672/how-to-convert-string-to-unsigned-long-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!