【问题标题】:c++ std::istringstream weird std::hex behaviourc++ std::istringstream 奇怪的 std::hex 行为
【发布时间】:2014-09-14 14:34:05
【问题描述】:

编辑:

我设法在较小的范围内解决了同样的问题:

std::istringstream hex;
std::string str = "0x7ffa428ab946";
std::cout << "str " << str << std::endl;
hex.str(str);
long caller;
hex >> std::hex >> caller;
std::cout << "caller " << caller << std::endl;
str = "0x7ff9ec0010f0";
std::cout << "str " << str << std::endl;
hex.str(str);
long address;
hex >> std::hex >> address;
std::cout << "address " << address << std::endl;

然后得到这个:

str 0x7ffa428ab946
caller 140712834939206
str 0x7ff9ec0010f0
address 0

这是为什么呢?

【问题讨论】:

  • “这和量子力学有关系吗?”这到底和量子力学有什么关系?
  • @P0W 抱歉,这是个小玩笑,因为我不知道是什么原因造成的......
  • 听起来这个问题可以缩短到一两行代码
  • @keyser 我做到了,不过超过 2 行。
  • hex.str(str) 无法清除 eofbit,因此第二次提取将失败。在通话后添加hex.clear();

标签: c++ hex istringstream


【解决方案1】:
hex >> std::hex >> caller;

将在hex 上设置eofbit,但随后

hex.str(str);

没有清除它。因此,以后从hex 中提取的尝试将完全失败。

在调用hex.str(str); 后调用hex.clear() 以清除标志。

【讨论】:

    猜你喜欢
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    • 2020-06-11
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多