Raised This Month: $12 Target: $400
 3% 

Decode URI encoded string


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
DotNetJunkie
Senior Member
Join Date: May 2005
Location: In front of my pc
Old 10-21-2006 , 13:42   Decode URI encoded string
Reply With Quote #1

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

Code:
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;
}
__________________
DotNetJunkie is offline
Send a message via ICQ to DotNetJunkie Send a message via AIM to DotNetJunkie Send a message via MSN to DotNetJunkie Send a message via Yahoo to DotNetJunkie
Zenith77
Veteran Member
Join Date: Aug 2005
Old 10-21-2006 , 13:48   Re: Decode URI encoded string
Reply With Quote #2

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).
__________________
Quote:
Originally Posted by phorelyph View Post
your retatred

Last edited by Zenith77; 10-21-2006 at 13:51.
Zenith77 is offline
DotNetJunkie
Senior Member
Join Date: May 2005
Location: In front of my pc
Old 10-21-2006 , 13:58   Re: Decode URI encoded string
Reply With Quote #3

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.
__________________

Last edited by DotNetJunkie; 10-21-2006 at 14:01.
DotNetJunkie is offline
Send a message via ICQ to DotNetJunkie Send a message via AIM to DotNetJunkie Send a message via MSN to DotNetJunkie Send a message via Yahoo to DotNetJunkie
Zenith77
Veteran Member
Join Date: Aug 2005
Old 10-21-2006 , 14:06   Re: Decode URI encoded string
Reply With Quote #4

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 . I will defiantly use this in a plugin I'm making.
__________________
Quote:
Originally Posted by phorelyph View Post
your retatred
Zenith77 is offline
Greenberet
AMX Mod X Beta Tester
Join Date: Apr 2004
Location: Vienna
Old 10-21-2006 , 14:07   Re: Decode URI encoded string
Reply With Quote #5

Quote:
Originally Posted by DotNetJunkie View Post
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"
Greenberet is offline
Send a message via ICQ to Greenberet Send a message via MSN to Greenberet
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 18:11.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode