Converting Hexadecimal characters to integers.

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

Join Date: Sep 2004
Posts: 2
Reputation: JECMAIL is an unknown quantity at this point 
Solved Threads: 0
JECMAIL JECMAIL is offline Offline
Newbie Poster

Converting Hexadecimal characters to integers.

 
0
  #1
Sep 29th, 2004
I have to build a C++ code that accepts 4 separate Hexadecimal inputs and returns in English the human readable for a set of machine code instructions. I can either get my program to accept characters or integers. At this time I have my program set to work on integers from 0-9. That leaves me with A,B,C,D,E and F that will not work in my program. Is there a way to convert a user entered (cin) char to an integer?

Any help would be greatly appreciated.

John
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,459
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 252
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Converting Hexadecimal characters to integers.

 
0
  #2
Sep 29th, 2004
>Is there a way to convert a user entered (cin) char to an integer?

Why convert it, why not just use the chars '0', '1', ... 'A', 'B', ... ?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 2
Reputation: JECMAIL is an unknown quantity at this point 
Solved Threads: 0
JECMAIL JECMAIL is offline Offline
Newbie Poster

Re: Converting Hexadecimal characters to integers.

 
0
  #3
Sep 29th, 2004
Originally Posted by Dave Sinkula
>Is there a way to convert a user entered (cin) char to an integer?

Why convert it, why not just use the chars '0', '1', ... 'A', 'B', ... ?
I did use characters at first. But when I use the switch statement, I need to compare the numbers for various case statements. I rely heavily on the arithmetic operations >,<,=,!=. If I can not use these operations my program will really be large. I was hoping that there was another way.

John
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,847
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: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Converting Hexadecimal characters to integers.

 
0
  #4
Sep 30th, 2004
>Is there a way to convert a user entered (cin) char to an integer?
If you want to read each digit separately then it's a lot harder. Your best bet would be to read the entire number as a string and then parse it:
  1. #include <cctype>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. string s;
  10.  
  11. if ( getline ( cin, s ) ) {
  12. static string hex_digits ( "0123456789ABCDEF" );
  13.  
  14. string::const_iterator it = s.begin();
  15. string::const_iterator end = s.end();
  16.  
  17. for ( ; it != end; it++ ) {
  18. string::size_type val = hex_digits.find ( toupper ( *it ) );
  19.  
  20. if ( val != string::npos )
  21. cout<<"The value of "<< *it <<" is "<< val <<endl;
  22. }
  23. }
  24. }
New members chased away this month: 4
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC