boost::spirit::qi - 这个十六进制解析有什么问题?
boost::spirit::qi - whats wrong with this hex parse?
考虑以下代码:
{
std::string input = "FFFFFFF";
int result = 0;
auto itBeg = input.cbegin();
auto itEnd = input.cend();
if(!bsq::parse(itBeg, itEnd, bsq::int_parser<int, 16>(), result) || itBeg != itEnd)
{
throw std::exception();
}
std::cout << input << " means " << result << std::endl;
}
{
std::string input = "FFFFFFFF";
int result = 0;
auto itBeg = input.cbegin();
auto itEnd = input.cend();
if(!bsq::parse(itBeg, itEnd, bsq::hex, result) || itBeg != itEnd)
{
throw std::exception();
}
std::cout << input << " means " << result << std::endl;
}
{
std::string input = "FFFFFFFF";
int result = 0;
auto itBeg = input.cbegin();
auto itEnd = input.cend();
if(!bsq::parse(itBeg, itEnd, bsq::int_parser<int, 16>(), result) || itBeg != itEnd)
{
throw std::exception();
}
std::cout << input << " means " << result << std::endl;
}
第一次解析在 0xFFFFFFF 上工作正常(注意,7 个十六进制数字)
第二个在 0xFFFFFFFF(8 个十六进制数字)上工作正常
第三个失败了,我不明白为什么。 int_parser<T, 16>
和bsq::hex 本质上不是一样的吗?
生活在 Coliru
the third one fails and I cant understand why
您正在使用签名的解析器,并且达到了类型宽度边界。将其更改为:
bsq::uint_parser<unsigned int, 16>(), result)
(live demo)
isnt int_parser<T, 16>
is essentially the same as bsq::hex
?
没有
The documentation 说 boost::spirit::qi::hex
会
Parse an unsigned integer using [..] radix 16
考虑以下代码:
{
std::string input = "FFFFFFF";
int result = 0;
auto itBeg = input.cbegin();
auto itEnd = input.cend();
if(!bsq::parse(itBeg, itEnd, bsq::int_parser<int, 16>(), result) || itBeg != itEnd)
{
throw std::exception();
}
std::cout << input << " means " << result << std::endl;
}
{
std::string input = "FFFFFFFF";
int result = 0;
auto itBeg = input.cbegin();
auto itEnd = input.cend();
if(!bsq::parse(itBeg, itEnd, bsq::hex, result) || itBeg != itEnd)
{
throw std::exception();
}
std::cout << input << " means " << result << std::endl;
}
{
std::string input = "FFFFFFFF";
int result = 0;
auto itBeg = input.cbegin();
auto itEnd = input.cend();
if(!bsq::parse(itBeg, itEnd, bsq::int_parser<int, 16>(), result) || itBeg != itEnd)
{
throw std::exception();
}
std::cout << input << " means " << result << std::endl;
}
第一次解析在 0xFFFFFFF 上工作正常(注意,7 个十六进制数字)
第二个在 0xFFFFFFFF(8 个十六进制数字)上工作正常
第三个失败了,我不明白为什么。 int_parser<T, 16>
和bsq::hex 本质上不是一样的吗?
生活在 Coliru
the third one fails and I cant understand why
您正在使用签名的解析器,并且达到了类型宽度边界。将其更改为:
bsq::uint_parser<unsigned int, 16>(), result)
(live demo)
isnt
int_parser<T, 16>
is essentially the same asbsq::hex
?
没有
The documentation 说 boost::spirit::qi::hex
会
Parse an unsigned integer using [..] radix 16