URL Decoding

Decodes URL-encoded string



									/*****Please include following header files*****/
// math.h
/***********************************************/

int HexadecimalToDecimal(char* hex, int len)
{
	int hexLength = len;
	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;
}

char* URLDecoding(char* data, unsigned int count) {
	char* result = malloc(count);
	int j = 0;

	for (int i = 0; i < count; ++i, ++j)
	{
		if (data[i] == '%')
		{
			char h[] = { data[i + 1], data[i + 2] };
			result[j] = (char)HexadecimalToDecimal(h, 2);
			i += 2;
		}
		else
		{
			result[j] = data[i];
		}
	}

	result[j] = '\0';

	return result;
}
								


Example

									char* data = "jdfgsdhfsdfsd%206445dsfsd7fg%2F%2A%2F%2Bbfjsdgf%25%24%5E";
char* value = URLDecoding(data, strlen(data));
								


Output

									jdfgsdhfsdfsd 6445dsfsd7fg/*/+bfjsdgf%$^