tags:

views:

86

answers:

3

How do I escape strings in C#? I have strings which are the bytes from a PNG and I need to escape them correctly in code to avoid compile errors...Any ideas?

So here is the type of code, I have which doesn't compile

public const string s =

"wewegliwewejwqejsadaskjda" +

"wewegliwewejqejsadaskjda" +

"wewegliwewejejsadaskjda" ;

A: 

\ is the escape character for C#.

Guster_Q
+1  A: 

Do you mean a Base 64 string? Usually if you see a PNG in string form, its base 64.

If not base 64, what type of encoding? Can you give us an example of what these strings look like?

EDIT:

To convert a Base64 string to a byte array (from which you can either save it as a PNG file or open it as an Image object) do this:

byte[] filebytes = Convert.FromBase64String(yourBase64String);
Neil N
Yes, base 64, I just edited my post with an example
Nevin Mathai
You dont need to escape Base 64, I'm guessing you need to convert them to an image?
Neil N
+1  A: 

The code you've given will compile, except that you've used constant instead of const. (Admittedly that's not valid Base64, as it contains the padding = character in the middle rather than at the end.) If this doesn't help, please post an example with this problem corrected but that still doesn't compile.

As Neil said, none of the characters in Base64 need escaping in C#.

Jon Skeet
I'm curious if he was trying to paste an entire PNG as Base64 into C#. What is the limit for string literals? I tried this once back in my VB6 days and it didnt turn out so well.
Neil N