944,048 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6387
  • C++ RSS
Sep 29th, 2004
0

Converting Hexadecimal characters to integers.

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JECMAIL is offline Offline
2 posts
since Sep 2004
Sep 29th, 2004
0

Re: Converting Hexadecimal characters to integers.

>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', ... ?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 29th, 2004
0

Re: Converting Hexadecimal characters to integers.

Quote 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JECMAIL is offline Offline
2 posts
since Sep 2004
Sep 30th, 2004
0

Re: Converting Hexadecimal characters to integers.

>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:
C++ Syntax (Toggle Plain Text)
  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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Plz. Help me get my program to work
Next Thread in C++ Forum Timeline: A little Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC