How to read numeric data like uint8_t

I have human readable numeric data in istream . Values ​​range from 0 to 255, and I want to save them in uint8_t . Unfortunately, if I try something like

 uint8_t a, b; stringstream data("124 67"); data >> a >> b; 

then I get a == '1' and b == '2' . I understand that in many situations this is the desired behavior, but I want to get a == 124 and b == 67 . My current solution is to stream data to int s, then copy them to uint8_t s.

 uint8_t a, b; int a_, b_; stringstream data("124 67"); data >> a_ >> b_; a = a_; b = b_; 

Clearly, this is very cumbersome (and a bit inefficient). Is there a cleaner way to read the numeric (as opposed to characters) uint8_t data using stream s?

+5
source share
1 answer

You can not. uint8_t and int8_t are typedefs for unsigned char and signed char respectively. These types are considered iostreams character types, and there is no way to change this behavior.

The second example is the only way to do this.

+5
source

Source: https://habr.com/ru/post/1200174/


All Articles