如何在C ++中将字符串转换为unsigned long? [英] How to convert string to unsigned long in C++?

查看:498
本文介绍了如何在C ++中将字符串转换为unsigned long?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编程方面的帮助.该程序必须接受一个字符串,该字符串最终将变为无符号长.但是这里有个问题,必须有一个错误捕获器,当您输入十六进制和诸如a!!!!!!之类的符号的组合时,将产生一个错误,并且无符号long变量必须能够接受和存储大于4294967295的输入,即FFFFFFFF.我已经尝试过以下代码段:

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);

我的问题是,当我输入FFFFFFFFF(9个F)而不是FFFFFFFF(8个F)时,持有人仍会接受4294967295,即使它大于.另一件事是,当我同时结合十六进制和符号a!!!!!时,fgets仍会考虑十六进制A.

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!

推荐答案

因此,如果您查看本文档中的 strtoul ,您将在Return Value部分下看到此内容:

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

如果转换后的值超出相应返回类型的范围,则发生范围错误,并返回ULONG_MAX或ULLONG_MAX.

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)

对于a!!!!案例,回顾同一文档,您将看到:

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

函数将str_end指向的指针设置为指向最后解释的字符之后的字符.如果str_end为NULL,则将其忽略.

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.

您当前正在传递NULL,但是如果传递参数:

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

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

您现在可以检查*p是否为NULL终止符,如果是,则处理所有字符,否则您会遇到问题.

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.

您还可以选择使用 stoul 如果无法执行任何转换,则为std::invalid_argument,如果转换后的值将不在结果类型的范围内,则为std::out_of_range.

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将是最后处理的字符的索引,在您的情况下,如果pos != str1.length()则无法处理整个字符串并且您有问题.

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.

这篇关于如何在C ++中将字符串转换为unsigned long?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆