Hexadecimal To ASCII

This algorithm converts hexadecimal numbers to ASCII code.



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

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

static int HexadecimalToDecimal(string hex) {
	int hexLength = hex.length();
	double dec = 0;

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

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

		dec += b * pow(16, ((hexLength - i) - 1));
	}

	return (int)dec;
}

static string HexadecimalToASCII(string hex) {
	string ascii = "";
	int hexLen = hex.length();

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

	return ascii;
}
								


Example

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


Output

									Programming Algorithms