# Read from string

• 02-19-2006
rabbit
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?
• 02-19-2006
Ancient Dragon
>>I don't know how to start

Hint:
Code:

```#include <string> #include <iostream> int main() {   // put your code here }```
• 02-19-2006
rabbit
Sorry, maybe i was to vague.

Code:

```#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; }```
The String will look something like this

A347F2B74EE9A9F6

I want to read every two characters, ( they're already in hex form) then convert to int.
• 02-19-2006
0rion
You need to use a loop to traverse through the string and extracting 2 characters until it reaches the end of the string.
• 02-19-2006
rabbit
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.
• 02-19-2006
laserlight
Quote:

What I'm doing now is taking the value of A and adding it to the value of 3, which is not 163.
You need to multiply by 16 before adding. Of course the value of 0xA is 10, not 65.
• 02-19-2006
0rion
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.