decimal 255 as hex displaying as FFFFFFFF

Oct 9, 2016 at 10:16pm
Hi guys,

Small problem i'm having when playing around with hex, decimal and binary values. I'm trying to output the decimal number 255 as hex and binary, the binary is displaying correctly however the hex value is outputting "FFFFFFFF". I'll post my code below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <bitset>
#include <iomanip>

using namespace std;
	

int8_t a = 255;
int8_t b = 85;

int main()
{
	cout << "dec: 255 = " << bitset<8>(a) << endl;
	cout << "dec: 255 = " <<  hex << +a << endl << endl;


}


I've looked online for similar problems however can't find one that's specific. I'm assuming it's a small error i've made but cannot identify it.

Thanks for having a peep!
Oct 9, 2016 at 10:35pm
An int8_t is signed, and so does not have enough bits to represent +255. Perhaps your compiler is giving you a warning about this? I would try with uint8_t instead.
Oct 9, 2016 at 11:11pm
Thanks alot that actually fixed the problem!
Topic archived. No new replies allowed.