Hi, I need to read every two characters from a string inputted by the user, then convert that from hex to decimal form. I don't know how to start this so can someone please help?
Hi, I need to read every two characters from a string inputted by the user, then convert that from hex to decimal form. I don't know how to start this so can someone please help?
>>I don't know how to start
Hint:
Code:#include <string> #include <iostream> int main() { // put your code here }
Sorry, maybe i was to vague.
The String will look something like thisCode:#include <iostream> #include <string.h> using namespace std; int main() { int intpass; string enpass, hexpass; cin >> hexpass; intpass = (int(hexpass.at(0)) + (hexpass.at(1))); cout << hex << intpass; cin.ignore(); cin.get(); return 0; }
A347F2B74EE9A9F6
I want to read every two characters, ( they're already in hex form) then convert to int.
You need to use a loop to traverse through the string and extracting 2 characters until it reaches the end of the string.
Last edited by 0rion; 02-19-2006 at 10:02 AM.
The cost of software maintenance increases with the square of the programmer's creativity.
I was going to use a loop, but the thing is A3 in decimal 163. What I'm doing now is taking the value of A and adding it to the value of 3, which is not 163. I don't know how to store 2 characters like this.
You need to multiply by 16 before adding. Of course the value of 0xA is 10, not 65.What I'm doing now is taking the value of A and adding it to the value of 3, which is not 163.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I would probably try and create another function that converts hex to decimal that takes 2 characters and returns back the decimal value. You could use a mini-symbol table to link up 'A' to 10, 'B' to 11 etc. and then just do "firstdigit * 16 + seconddigit" and return that.
The cost of software maintenance increases with the square of the programmer's creativity.