PDA

View Full Version : Decode URI encoded string


DotNetJunkie
10-21-2006, 13:42
This 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.

You'll need the HexToDec function found here:
http://forums.alliedmods.net/showthread.php?t=46216


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;
}

Zenith77
10-21-2006, 13:48
Ok when posting a tutorial include more information. Such as:


What an URI encoded string is.
What they are used for.
Explaintion of your code (kind of like step by step).

DotNetJunkie
10-21-2006, 13:58
Link wasn't intended to be a valid link. I placed "e.g." next to it which stands
for Example Given. A URI encoded string is usually used by the HTTP protocol
to use special characters which are not permitted by the protocol.

Say we have the space character which in hexadecimal is represented by 20
and is 32 in decimal ASCII however a HTTP request looks like this:

GET /some file.txt HTTP/1.1

as we can see the server needs to parse this message and obviously we
cannot use a space because it would screw up the parsing since the server
will be using a space as the delimiter so in order to access a filename with a
space on the server we would encode the URI to look like this:

GET /some%20file.txt HTTP/1.1

Now the server can correctly parse the request without any problems however
now it needs to decode the URI encoded string, in order to do this the server
examines the string until it finds a '%' character meaning that we've reached
an encoded character so the server grabs the next two characters and converts
the given hexadecimal number, which in this example is 20, and converts it to
an ASCII character which is 32.

This function would take a string that looks like:
%48ello%2DWorld%21
and convert it to its unencoded form:
Hello-World!

Unfortunately I have not written an encoding function yet but hopefully you
get the idea.

But overall URI encoded strings are usually used to make what would be
over-complicated parsing into a simple one.

Zenith77
10-21-2006, 14:06
I was hoping you would put that in your original tuturial but oh well :). About the link thing, you'll see that I edited my post because I saw .txt at the end assumed it was some file with examples or something, but I realized what it was and quickly fixed my mistake ^^.

Anyways good job :up:. I will defiantly use this in a plugin I'm making.

Greenberet
10-21-2006, 14:07
Link wasn't intended to be a valid link. I placed "e.g." next to it which stands
for Example Given.
wrong... "e.g." stands for "Exempli gratia"