# what datatype do I assign hexadecimal input

• 11-17-2002
m712
what datatype do I assign hexadecimal input
The user inputs a hexadecimal number. What data type do I declare the user's input to be...string? or...?

I'm going to be converting it to the corresponding decimal place number.
• 11-17-2002
Imperito
So why not input it as an integer?
• 11-17-2002
m712
is that OK?? I thought it wouldn't be because hexadecimals include a,b,c,d, and e. But I don't know, is it OK?
• 11-17-2002
swoopy
One way is to input it as an integer. Input it as hexadecimal with the hex specifier.
Code:

```int num; cout << "Enter number in hex:"; cin >> hex >> num;```
• 11-17-2002
m712
Thanks to both of you! That makes things so much easier.

Also, if it's not too much to ask, I was curious to know whether I need to know how many digits/characters the hexadecimal number( user input) is and how to navigate each character...is there something for integers like inputstr[] for strings?

I was thinking of using each chracter place value a power of 16 multiplied by the corresponding placevalue and summing those
• 11-17-2002
face_master
Quote:

Originally posted by swoopy
One way is to input it as an integer. Input it as hexadecimal with the hex specifier.
Code:

```int num; cout << "Enter number in hex:"; cin >> hex >> num;```

Cool! What other 'specifiers' are there?
• 11-17-2002
swoopy
I learned the hex specifier from another thread here. I knew you could use it for output, but didn't know you can also use it for input.

Well, there's dec (for decimal which is the default of course), oct, and hex. No bin. :(
• 11-17-2002
swoopy
It depends on the machine how long an int is. You can use sizeof(int) to find out. On my computer it's 4 bytes and a long is also 4 bytes. So to print the whole int in hex, you can use printf().
Code:

```  int num;   cout << "Enter number in hex:";   cin >> hex >> num;   cout << "num:" << num << endl;   printf("%08X\n",num);```
You can also store the hex number as a string with sprintf().
Code:

```  char buf[20];   sprintf(buf,"%08X",num);   cout << buf << endl;```
• 11-17-2002
m712
I'm so sorry, I made a mistake; the program works correctly. I am going to delete that post.
• 11-17-2002
Sang-drax
Just a thought: Why on earth doesn't the standard specify a bin modifier?
• 11-17-2002
Imperito
It mght have something to do with the fact that negative binary numbers are represented differently than negaitve numbers in other bases.
• 11-17-2002
Prelude
>Just a thought: Why on earth doesn't the standard specify a bin modifier?
If you can find a way to portably do it then we'd be glad to hear it. You have to consider unsigned values, obscure signed representations as well as sign-magnitude, one's complement, and two's complement, positive and negative values, big-endian, little-endian, mixed-endian and other obscure endianness representations, the list goes on. Binary representations are just too varied to easily create that kind of functionality.

-Prelude
• 11-17-2002
666
Surely if the cout statement knew the data type and the storage of that particular type i.e. signed or unsigned it would be able to call the appropriate binary function to deal with that ie.

Code:

```int main(void) {   unsigned char Num = 125;   cout << bin << Num;   return 0;  }```
There maybe some standards that would need to be adopted before it was fully implemented, i.e. whether to use 2's complement or a bias etc to display numbers.