ASCII To Hexadecimal

This algorithm converts ASCII code to hexadecimal numbers.



									public static string ASCIIToHexadecimal(string str)
{
	string hex = string.Empty;

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

		if (chex.Length == 1)
			chex = chex.Insert(0, "0");

		hex += chex;
	}

	return hex;
}

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

	int hex = dec;
	string hexStr = string.Empty;

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

		if (hex < 10)
			hexStr = hexStr.Insert(0, Convert.ToChar(hex + 48).ToString());
		else
			hexStr = hexStr.Insert(0, Convert.ToChar(hex + 55).ToString());

		dec /= 16;
	}

	return hexStr;
}
								


Example

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


Output

									50726F6772616D6D696E6720416C676F726974686D73