reading a hexadecimal number

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2006
Posts: 40
Reputation: beuls is an unknown quantity at this point 
Solved Threads: 2
beuls beuls is offline Offline
Light Poster

reading a hexadecimal number

 
0
  #1
Mar 24th, 2006
hi,

how can you read a hexadecimal number in c++? is there any option with cin/

your help is greatly appreciated.

Beulah
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: reading a hexadecimal number

 
0
  #2
Mar 24th, 2006
  1. #include <iostream.h>
  2.  
  3.  
  4. void main() {
  5. int n;
  6. while (cin >> n) {
  7. cout << "decimal: " << n << endl;
  8.  
  9. //--- Print hex with leading zeros
  10. cout << "hex : ";
  11. for (int i=2*sizeof(int) - 1; i>=0; i--) {
  12. cout << "0123456789ABCDEF"[((n >> i*4) & 0xF)];
  13. }
  14. cout << endl << endl;
  15. }
  16.  
  17. }

That's reading a int and converting it to hex. Can u figure out how to do da opposite?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,603
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: reading a hexadecimal number

 
0
  #3
Mar 24th, 2006
>how can you read a hexadecimal number in c++? is there any option with cin/
The std::hex modifier works with input streams:
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. int x;
  6.  
  7. std::cin>> std::hex >> x;
  8. std::cout<< x <<'\n';
  9. }
Type in 1B (or 1b), and the output will be 27, which is a correct conversion from hexadecimal input to decimal output.

>That's reading a int and converting it to hex.
That's evil.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: reading a hexadecimal number

 
0
  #4
Mar 24th, 2006
>That's evil

I know wud u expect anything less from me?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 40
Reputation: beuls is an unknown quantity at this point 
Solved Threads: 2
beuls beuls is offline Offline
Light Poster

Re: reading a hexadecimal number

 
0
  #5
Mar 27th, 2006
[QUOTE=Narue]>how can you read a hexadecimal number in c++? is there any option with cin/
The std::hex modifier works with input streams:
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. int x;
  6.  
  7. std::cin>> std::hex >> x;
  8. std::cout<< x <<'\n';
  9. }
Type in 1B (or 1b), and the output will be 27, which is a correct conversion from hexadecimal input to decimal output.

Thank you

Beulah
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC