LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 12-07-2001, 10:38 AM   #1
kud0ze
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.

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

Rep: Reputation: 47
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);
else
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));
}
else
{
str[bpos++] = instr[ipos];
}

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, 12:38 PM   #3
kud0ze
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.

http://www.network-tools.com/

---
kud0ze
 
  


Reply



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 02:53 AM
URL-Encoding on the command line? MikeyCarter Linux - Software 2 09-27-2005 08:10 AM
ERROR The requested URL could not be retrieved While trying to retrieve the URL: /re Niceman2005 Linux - General 1 06-29-2005 09:51 AM
Other than US encoding kornerr Linux - General 4 01-21-2005 09:42 AM
url encoding doesn't work fine with PHP markus1982 Programming 0 08-30-2003 02:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:00 AM.

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