I understand that integers are stored in binary notation, but I was wondering how this affects the reading of them - for example:
Assuming
cin.unsetf(ios::dec); cin.unsetf(ios::hex); and cin.unsetf(ios::oct);
the user inputs
0x43 0123 65
which are stored as integers. Now assume that the program wants to recognize these values as hex, oct, or dec and does something like this.
void number_sys(int num, string& s)
{
string number;
stringstream out;
out << num;
number = out.str();
if(number[0] == '0' && (number[1] != 'x' && number[1] != 'X')) s = "octal";
else if(number[0] == '0' && (number[1] == 'x' || number[1] == 'X')) s = "hexadecimal";
else s = "decimal";
}
the function will read all of the integers as decimal. I put in some test code after the string conversion to output the string, and the string is the number in decimal form. I was wondering if there is a way for integers to keep their base notation.
Of course you could input the numbers as strings and test that way, but then there is the problem of reading the string back as an int.
For example:
string a = 0x43;
int num = atoi(a.c_str());
cout << num; // will output 43
It seems like keeping/converting base notation can get very tricky. As an added problem, the hex, dec, and oct manipulators wouldn't even help with the issue shown above since the integer is being stored completely incorrectly, it's not even converting to a decimal.