//Convert a string to hexidecimmal and vice versa

//By DreamVB
#include <iostream>
#include <sstream>
#include <iomanip>

using namespace std;
using std::cout;
using std::endl;

string StrHex(string src){
	string buffer = "";
	ostringstream os;

	//Convert string of text to hexidecinal string.
	for (string::size_type x = 0; x < src.length(); ++x){
		//Line below converts the src[x] char to hex and also sets width to 2
		os << std::hex << std::setfill('0') << setw(2) << (int)src[x];
	}

	return os.str();
}

string HexToStr(string src){
	string Ret = "";
	int x = 0;

	//Check that string is equal size.
	if (src.length() % 2 != 0){
		return "";
	}
	//Convert hex string to a string
	while (x < src.length()){
		Ret += (char)strtoul(src.substr(x, 2).c_str(), NULL, 16);
		//INC counter
		x += 2;
	}
	return Ret;
}


int main(int argc, char *argv[]){
	string msg = "This is top secret.";

	cout << "Original  : " << msg << endl;
	cout << "Encoded   : " << StrHex(msg) << endl;
	cout << "Decoded   : " << HexToStr("5468697320697320746f70207365637265742e") << endl;

	system("pause");
	return 0;
}