• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2283
  • Last Modified:

Unescape function for C/C++ CGI program.

Does anyone know of a site where I can get the source code to convert the escaped non-alphanumeric characters back to its original form, just like the unescape() function in JavaScript?
0
szetoa
Asked:
szetoa
1 Solution
 
bebonhamCommented:

##sample script
use URI::Escape;
$string="what's up";
$string=uri_escape($string);
print "$string\n";
print uri_unescape($url_encode_string);
##end
that would be the most comparable...

but you can also use a regexp or pack and unpack to do this the 'long' way.

0
 
marecsCommented:
This is my own version. It uses the utility function ostrcpy which is like strcpy but is defined for strings with overlapping memory, provided the second string's pointer is > than the first string's pointer. This routine does in place unescaping.

/* Strings with overlapping memory copy */
void ostrcpy (char *str1, char *str2) {
   char *p1, *p2;
   for (p1=str1,p2=str2;;p1++,p2++) {
      *p1=*p2;
      if (*p1 == '\0') break;
   }
}

char * unescape (char *string) {
    char hex[2];
    char *dummy, *p;
   /* Do actual character translation in place */
    for (p=string;*p != '\0';p++)
        if (*p == '%') {
            hex[0] = *(p+1);
            hex[1] = *(p+2);
            *p = (char) strtol(hex, &dummy, 16);
            ostrcpy(p+1,p+3);
         }
    return string;
}


0
 
szetoaAuthor Commented:
bebonham

Thanks for your help.  But my CGI is a C/C++ program.  So I was looking for C/C++ approach.

marces

Thank you for your help.  It works.

Thanks again experts.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now