Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::string to int8_t

Tags:

c++

This gives a bad_lexical_cast exception:

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

I would like to have an exception when the value doesn't fit in an int8_t.

How should I do this? Should I cast to an int first and throw an exception if the value falls outside the range -128 to 127?

I'm also interested in converting strings to uint8_t.

like image 589
Baz Avatar asked Nov 30 '25 16:11

Baz


1 Answers

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

Source:

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

like image 102
tenfour Avatar answered Dec 03 '25 07:12

tenfour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!