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

6
  • Interesting question - it may be worth showing what python outputs for this so that C# only developers have a bit more context. Commented Jul 2, 2013 at 15:01
  • s.decode("string-escape") should produce a string that is suitable as string literal in Python source code. Commented Jul 2, 2013 at 15:06
  • You want to un-escape a python string literal with C#? Commented Jul 2, 2013 at 15:14
  • 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. Commented Jul 2, 2013 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 Commented Jul 2, 2013 at 15:37

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.