Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

stringstream

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'

like image 948
Luke Avatar asked Feb 23 '13 23:02

Luke


2 Answers

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"
like image 138
aleksandrm8 Avatar answered Oct 27 '22 02:10

aleksandrm8


Cast it to an int:

std::cout << (int)s << " equals 2 not 255 " << std::endl;
like image 22
nneonneo Avatar answered Oct 27 '22 02:10

nneonneo