views:

190

answers:

2

I'm working on an application that needs to accept posted data from a form and process it. One step of this process is to unescape the data that comes in. One issue that I'm facing is that the data I'm grabbing from the form is binary in nature so it includes escape sequences that I need to turn back into characters. This is fairly trivial with characters like tab and newline, but I can't figure out how to do this with hex and octal values.

Here's an example of some input data:

"blahblah\nblahblah\x20blahblah\037blahblah"

When it gets posted it'll look something like:

"blahblah%5Cnblahblah%5Cx20blahblah%5C037blahblah"

For the most part I'm currently just going through the string and scanning for '%'. Then I use a sscanf to get the value of the escaped character. Then if it's 92, I look at the next character. If it's something like 'n', I just replace the characters with '\n' and continue.

My question is basically how can I scan through the string for hex and octal values? In the example above, how could I get to %5C037 and replace that whole sequence with the appropriate '\037' character?

As a note, I have to do all of this because the data accepted on the form is usually passed around between server calls and I'm trying to put together a test application to allow a user to see what is happening to the data.

+2  A: 

strtol should do what you need.

Brian Campbell
This should work. While it doesn't really give me the \xxx representation that would be ideal, it does let me pull out the character value.
bcasp
+4  A: 

Google for url decode C implementation and you will find many answers, one of them:

http://www.icosaedro.it/apache/urldecode.c