decimal 255 as hex displaying as FFFFFFFF

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!
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.
Thanks alot that actually fixed the problem!
Topic archived. No new replies allowed.