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.
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.
So why not input it as an integer?
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?
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;
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
Last edited by m712; 11-17-2002 at 01:48 AM.
Cool! What other 'specifiers' are there?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;
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.
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().
You can also store the hex number as a string with sprintf().Code:int num; cout << "Enter number in hex:"; cin >> hex >> num; cout << "num:" << num << endl; printf("%08X\n",num);
Code:char buf[20]; sprintf(buf,"%08X",num); cout << buf << endl;
I'm so sorry, I made a mistake; the program works correctly. I am going to delete that post.
Just a thought: Why on earth doesn't the standard specify a bin modifier?
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
It mght have something to do with the fact that negative binary numbers are represented differently than negaitve numbers in other bases.
>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
My best code is written with the delete key.
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.
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.Code:int main(void) { unsigned char Num = 125; cout << bin << Num; return 0; }