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?
Printable View
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.
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.Quote:
What I'm doing now is taking the value of A and adding it to the value of 3, which is not 163.
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.