Decode URI encoded string Converting Hexadecimal to Decimal
Posted: Sat Aug 15, 2026 2:59 am
If you are unfamiliar with hexadecimal numbers please look here:
CONVERTING_HEXADECIMAL_TO_DECIMAL.pdf
You'll need the HexToDec function found here:
The following is used to decode a URI encoded string like you often see in
web browsers e.g. http://someplace.com/some%20file.txt
I needed this for a project and didn't know of any function that could do it.
CONVERTING_HEXADECIMAL_TO_DECIMAL.pdf
You'll need the HexToDec function found here:
Code: Select all
public HexToDec(value[])
{
new string[32];
copy(string,sizeof(string)-1,value);
strtoupper(string);
new temp = 0;
new len = strlen(string);
new i = len;
while( i > 0 )
{
new dec = 0;
switch( string[i-1] )
{
case 'A': dec = 10;
case 'B': dec = 11;
case 'C': dec = 12;
case 'D': dec = 13;
case 'E': dec = 14;
case 'F': dec = 15;
case '0': dec = 0;
case '1': dec = 1;
case '2': dec = 2;
case '3': dec = 3;
case '4': dec = 4;
case '5': dec = 5;
case '6': dec = 6;
case '7': dec = 7;
case '8': dec = 8;
case '9': dec = 9;
}
temp += dec*power(16,(len-i));
i--;
}
return temp;
}web browsers e.g. http://someplace.com/some%20file.txt
I needed this for a project and didn't know of any function that could do it.
Code: Select all
public decodeURI(string[])
{
new len = strlen(string);
if( len <= 0 ) return 0;
new temp[512];
new hexstr[3] = { '0' , '0' , 0 };
new pos = 0;
new tpos = 0;
while( pos < len && tpos < sizeof(temp) )
{
if( string[pos] == '%' )
{
hexstr[0] = string[pos+1];
hexstr[1] = string[pos+2];
pos += 3;
temp[tpos] = HexToDec(hexstr);
tpos++;
continue;
}
temp[tpos] = string[pos];
pos++;
tpos++;
}
setc(string,len,0);
copy(string,len,temp);
return 1;
}