View the Most Wanted LQ Wiki articles.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-07-2001, 11:38 AM   #1
LQ Newbie
Registered: Oct 2001
Location: Dallas
Distribution: Slackware
Posts: 9

Rep: Reputation: 0
URL Encoding

Quick question...

Does anyone know of a utility or script out there that can convert escape characters to and from the Unicode equivalent?

For example:
%c0%af = /
%27 = '
... and vice versa.

I located a PDF that indicates that PERL can do UTF-8 conversions, but I'm a fledgling PERL coder and it's beyond my skill at this time.

A nudge in the right direction would be greatly appreciated.

Old 12-10-2001, 05:04 AM   #2
Senior Member
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 46
Here's a simple C procedure which does that. I suppose you could build a simple wrapper around it to make it usefull in a shell. It's only the encode function but the decode shouldn't be to hard if you just reverse the process.

static char* urlencode(const char *instr)
register int ipos, bpos; // input str pos., buffer pos.
static unsigned char *str = NULL;
int len = strlen(instr);
int tmp;

//attempt to reuse buffer
if (str == NULL)
str = (unsigned char *) malloc(3 * len + 1);
str = (unsigned char *) realloc(str, 3 * len + 1);

//malloc, realloc failed ?
if (errno == ENOMEM)
perror("Error allocating memory");
//return ref. to empty string, so's prog wont crash
return "";

ipos = bpos = 0;

while (ipos < len)
//using inverted logic from original code....
if (!isdigit((int) (instr[ipos]))
&& !isalpha((int) instr[ipos]) && instr[ipos] != '_')
tmp = instr[ipos] / 16;
str[bpos++] = '%';
str[bpos++] = ((tmp < 10) ? (tmp + '0') : (tmp + 'A' - 10));
tmp = instr[ipos] % 16;
str[bpos++] = ((tmp < 10) ? (tmp + '0') : (tmp + 'A' - 10));
str[bpos++] = instr[ipos];


str[bpos] = '\0';

//free extra alloc'ed mem.
tmp = strlen(str);
str = (unsigned char *) realloc(str, tmp + 1);

return (str);
Old 12-11-2001, 01:38 PM   #3
LQ Newbie
Registered: Oct 2001
Location: Dallas
Distribution: Slackware
Posts: 9

Original Poster
Rep: Reputation: 0
Thumbs up

Dank je, Mik. I didn't think anyone had an answer =)

Also, I found the following website, for anyone interested. It has functions for URL encoding and decoding, and much, much more.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Encoding of ,, Anden008 Linux - General 2 11-17-2005 03:53 AM
URL-Encoding on the command line? MikeyCarter Linux - Software 2 09-27-2005 09:10 AM
ERROR The requested URL could not be retrieved While trying to retrieve the URL: /re Niceman2005 Linux - General 1 06-29-2005 10:51 AM
Other than US encoding kornerr Linux - General 4 01-21-2005 10:42 AM
url encoding doesn't work fine with PHP markus1982 Programming 0 08-30-2003 03:04 AM

All times are GMT -5. The time now is 05:14 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration