3

Say I have this function in python, that takes a string and just returns the decode('string-escape') of it:

def myFunc(s):
    return s.decode("string-escape")

Is there an equivalent to this in C#? I've tried a few things like encoding the string into UTF-8 and storing it in a byte array, but nothing seems to give me the exact same string that python does.

EDIT: sample python usage:

>>> from base64 import b64encode     #import base64 encode
>>> s = "x\340s5g\272m\273\252\252\344\202\312\352\275\205" #original string
>>> decoded = s.decode("string-escape") 
>>> print decoded    #print decoded string
x?s5g?m?????꽅
>>> print b64encode(decoded)
eOBzNWe6bbuqquSCyuq9hQ==    #base 64 encoded version of the end result

So starting with the same string s in C#, how would I be able to get the same base64 encoded version?

If this isn't clear or needs more info please let me know

vik
  • 300
  • 1
  • 12
  • Interesting question - it may be worth showing what python outputs for this so that C# only developers have a bit more context. – robjohncox Jul 02 '13 at 15:01
  • `s.decode("string-escape")` should produce a string that is suitable as string literal in Python source code. – Ahmed KRAIEM Jul 02 '13 at 15:06
  • You want to un-escape a python string literal with C#? – Matthew Jul 02 '13 at 15:14
  • I have the same starting string in python and in C#. I have posted the way I got to the correct end result in python, and was wondering how I could get the same end result in C#. I'm not really sure if that's what you were asking – vik Jul 02 '13 at 15:29
  • 1
    Base 64 encoding is simple enough in C#, but I don't think there's a built-in encoder that can decode Python's string-escape format. You'd have to write that part yourself and then use Convert.ToBase64String to get the base64 encoded version. – RogerN Jul 02 '13 at 15:34
  • 2
    Hmm...do you know of any sources that might be helpful regarding how to decode Python's string-escape format? From my searches, all I can find are examples of using escape characters in strings – vik Jul 02 '13 at 15:37

1 Answers1

1

Looks if these methods helps

System.Text.ASCIIEncoding.ASCII.GetBytes()

System.Convert.ToBase64String()

Convert.FromBase64String()

Or what you may be looking for is

https://stackoverflow.com/questions/11584148/how-to-convert-a-string-containing-escape-characters-to-a-string

usually @ symbol before a string is what c# uses.

If you can clarify your questions it will be helpful

Community
  • 1
  • 1
Dexters
  • 2,419
  • 6
  • 37
  • 57