VB icon

StrToHex HexToStr v1.0

Email
Submitted on: 9/3/2016 8:55:23 PM
By: Ben128  
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general), Microsoft Visual C++, UNIX C++
Views: 2801
 
     Small project allows you to take a string of text and convert it to long long string of hexidecimal, then take the long hex string and convert it back to realable text. Hope you find it usfull.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: StrToHex HexToStr v1.0
// Description:Small project allows you to take a string of text and convert it to long long string of hexidecimal, then take the long hex string and convert it back to realable text. Hope you find it usfull.
// By: Ben128
//**************************************

//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 StrToHex(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: " << StrToHex(msg) << endl;
	cout << "Decoded: " << HexToStr("5468697320697320746f70207365637265742e") << endl;
	system("pause");
	return 0;
}


Other 50 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.