Hexadecimal To ASCII

This algorithm converts hexadecimal numbers to ASCII code.



									public static string HexadecimalToASCII(string hex)
{
	string ascii = string.Empty;

	for (int i = 0; i < hex.Length; i += 2)
	{
		ascii += (char)HexadecimalToDecimal(hex.Substring(i, 2));
	}

	return ascii;
}

private static int HexadecimalToDecimal(string hex)
{
	hex = hex.ToUpper();

	int hexLength = hex.Length;
	double dec = 0;

	for (int i = 0; i < hexLength; ++i)
	{
		byte b = (byte)hex[i];

		if (b >= 48 && b <= 57)
			b -= 48;
		else if (b >= 65 && b <= 70)
			b -= 55;

		dec += b * Math.Pow(16, ((hexLength - i) - 1));
	}

	return (int)dec;
}
								


Example

									string data = "50726F6772616D6D696E6720416C676F726974686D73";
string value = HexadecimalToASCII(data);
								


Output

									Programming Algorithms