Question

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.

Was it helpful?

Solution

strtol should do what you need.

OTHER TIPS

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top