9

I have this code and wondering if it's possible to have stringstream to treat 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'

Luke
  • 91
  • 1
  • 3

2 Answers2

4

If you are using std::stringstream in order to convert uint8_t to string, then you can use std::to_string instead. Allowed in c++11 only.

C++11 Feature

#include <stdint.h>
#include <iostream>
uint8_t value = 7;
std::cout << std::to_string(value) << std::endl;
// Output is "7"
Will Bickford
  • 5,381
  • 2
  • 30
  • 45
aleksandrm8
  • 320
  • 2
  • 5
0

Cast it to an int:

std::cout << (int)s << " equals 2 not 255 " << std::endl;
nneonneo
  • 171,345
  • 36
  • 312
  • 383