gpt4 book ai didi

c++ - std::string 到 int8_t

转载 作者:太空狗 更新时间:2023-10-29 23:41:15 25 4
gpt4 key购买 nike

这给出了一个 bad_lexical_cast 异常:

int8_t i = boost::lexical_cast<int8_t>("12");

当值不适合 int8_t 时,我想有一个异常(exception)。

我应该怎么做?如果值超出 -128 到 127 的范围,我应该先转换为 int 并抛出异常吗?

我也对将字符串转换为 uint8_t 感兴趣。

最佳答案

Q: What does lexical_cast of an int8_t or uint8_t not do what I expect?

A: As above, note that int8_t and uint8_t are actually chars and are formatted as such. To avoid this, cast to an integer type first

来源:

http://www.boost.org/doc/libs/1_51_0/doc/html/boost_lexical_cast/frequently_asked_questions.html

关于c++ - std::string 到 int8_t,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12472715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com