ASCII To Hexadecimal

This algorithm converts ASCII code to hexadecimal numbers.



									/*****Please include following header files*****/
// string
/***********************************************/

/*****Please use following namespaces*****/
// std
/*****************************************/

static string DecimalToHexadecimal(int dec) {
	if (dec < 1) return "0";

	int hex = dec;
	string hexStr = "";

	while (dec > 0)
	{
		hex = dec % 16;

		if (hex < 10)
			hexStr = hexStr.insert(0, string(1, (hex + 48)));
		else
			hexStr = hexStr.insert(0, string(1, (hex + 55)));

		dec /= 16;
	}

	return hexStr;
}

static string ASCIIToHexadecimal(string str) {
	string hex = "";
	int strLen = str.length();

	for (int i = 0; i < strLen; ++i)
	{
		string chex = DecimalToHexadecimal(str[i]);

		if (chex.length() == 1)
			chex = chex.insert(0, "0");

		hex += chex;
	}

	return hex;
}
								


Example

									string data = "Programming Algorithms";
string value = ASCIIToHexadecimal(data);
								


Output

									50726F6772616D6D696E6720416C676F726974686D73