1

My program outputs strings like "Wzyryrff}av{v5~fvzu: Bb``igbuz~+\177Ql\027}C5]{H5LqL{" and the problem is the escape codes (\\\ instead of \, \177 instead of the character, etc.)

I need a way to unescape the string of all escape codes (mainly just the \\\ and octal \027 types). Is there something that already does this?

Thanks

Reference: http://www.tailrecursive.org/postscript/escapes.html

The strings are an encrypted value and I need to decrypt them, but I'm getting the wrong values since the strings are escaped

user229044
  • 232,980
  • 40
  • 330
  • 338
Contra
  • 1,691
  • 15
  • 14

4 Answers4

3

It sounds more like it's encoded rather than simply escaped (if \177 is really a character). So, try decoding it.

Brad
  • 15,361
  • 6
  • 36
  • 57
  • This doesn't look like any _standard_ encoding I have ever seen, so there wouldn't be any built in decoder for it. – Oded Nov 23 '10 at 16:36
  • @Oded - It's allatori string encryption. Once I remove the octals and the escape codes I can run it through my decryptor and get the result plaintext – Contra Nov 23 '10 at 23:38
2

There is nothing built in to do exactly this kind of escaping.

You will need to parse and replace these sequences yourself.

The \xxx octal escapes can be found with a RegEx (\\\d{3}), iterating over the matches will allow you to parse out the octal part and get the replacement character for it (then a simple replace will do).

The others appear to be simple to replace with string.Replace.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • I thought maybe there would be something for this in .NET but I guess not. Once I get the octals with regex, am I going to need something to convert them or can I just typecast them as a char? – Contra Nov 23 '10 at 16:37
  • @Contra - I think you will need the decimal value before a cast. – Oded Nov 23 '10 at 16:38
0

If the string is encrypted then you probably need to treat it as binary and not text. You need to know how it is encoded and decode it accordingly. The fact that you can view it as text is incidental.

Polyfun
  • 9,479
  • 4
  • 31
  • 39
  • The way it is encrypted is just a simple xor operation on each character in the string, so getting it as binary for the decryption process wouldn't help. – Contra Nov 23 '10 at 23:39
-1

If you want to replace specific contents you can just use the .Replace() method.

i.e. myInput.Replace("\\", @"\")

I am not sure why the "\" is a problem for you. If it its actually an escape code then it just should be fine since the \ represents the \ in a string.

What is the reason you need to "remove" the escape codes?

Eliseo
  • 1,547
  • 1
  • 15
  • 14
  • @Eli, how does this change `\177` to it's rightful character? – Brad Nov 23 '10 at 16:33
  • The strings are an encrypted value and I need to decrypt them, but I'm getting the wrong values since the strings are escaped – Contra Nov 23 '10 at 16:35
  • @Brad, If the string shows as "\177" then should be able to replace it with the character it represents (which I have no idea what it is) ;) – Eliseo Nov 23 '10 at 16:39
  • @Eli, correct, but that means that you can't just `Replace("\\", @"\")` as you proposed. – Brad Nov 23 '10 at 16:40
  • @Brad, it seems I am not understanding what you are trying to accomplish here. If the problem your input or your output? I think your will probably have to look into into encoding/decoding it as Oded said. – Eliseo Nov 23 '10 at 16:46
  • @Eli, I'm not the OP. You mean looking to encoding/decoding as **@Brad** said? – Brad Nov 23 '10 at 17:10