Question

I wrote the code below which inputs a number in hex format and outputs it in decimal form:-

#include<iostream>
#include<iomanip>
#include<stdint.h>

using namespace std;

int main()
{
  uint8_t c;
  cin>>hex>>c;
  cout<<dec<<c;
  //cout<<sizeof(c);
  return 0;
}

But when I input c(hex for 12), the output was again c(and not 12). Can somebody explain?

Was it helpful?

Solution

This is because uint8_t is usually a typedef for unsigned char. So it's actually reading 'c' as ASCII 0x63.

Use int instead.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int c;
    cin>>hex>>c;
    cout<<dec<<c<<'\n';
    return 0;
}

Program output:

$ g++ test.cpp
$ ./a.out
c
12

OTHER TIPS

This is an unfortunate side effect of the fact that uint8_t is actually unsigned char. So when you store c, its storing the ASCII value of c (99 decimal), not the numeric value 12.

uint8_t is an alias of unsigned char and unfortunately ostream tries to output it as a character. This has been fixed in C++20 std::format:

#include <format>
#include <iostream>
#include <stdint.h>

int main() {
  uint8_t n = 42;
  std::cout << std::format("{}", n);
}

Output:

42
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top