C++ Programming Example

Convert to Hex

Converts a character or string into a hexidecimal string. The use of std::string removes any need for dynamic allocation and pointer maintenance. From:
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20525179.html

#include <string>
using namespace std;

string Byte2Hex(unsigned char c) {
const char *CnvTbl = "0123456789ABCDEF";
string S;

	S = CnvTbl[c >> 8];
	S += CnvTbl[c & 0x0F];
	return S;
	}

string String2Hex(const string &S) {
const int StrLen = S.length();
string Result;

	for (int i = 0; i < StrLen; ++i)
		Result  += Byte2Hex((unsigned char) S[i]);
	return Result;
	}