Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

1 byte integer doesn't convert i/o formats

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?

like image 202
AvinashK Avatar asked Dec 21 '25 05:12

AvinashK


2 Answers

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
like image 193
Dietrich Epp Avatar answered Dec 22 '25 20:12

Dietrich Epp


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.

like image 42
Doug T. Avatar answered Dec 22 '25 18:12

Doug T.