How to get stringstream to treat uint8_t as a number, not a character?

I have this code and am wondering if it is possible to have a stringstream to handle uint8_t as a number, not a character?

uint8_t s; std::stringstream sstream( "255" ); sstream >> s; std::cout << s << " equals 50/'2' not 255 " << std::endl; 

s should be 255 not 50 / '2'

+4
source share
2 answers

If you use std :: stringstream to convert uint8_t to string, you can use std :: to_string instead . Allowed only in C ++ 11.

C ++ 11 function

 #include <stdint.h> #include <iostream> uint8_t value = 7; std::cout << std::to_string(value) << std::endl; // Output is "7" 
+1
source

Move it to int :

 std::cout << (int)s << " equals 2 not 255 " << std::endl; 
0
source

All Articles