0

I have a problem with converting a byte array string back to a literal. I am reading a string from a xml file which was converted to a byte array (Encoding.Unicode.GetBytes(string)). I dose not developed the xml export. Now I would like to convert the value back to a string.

For example (note that it is a real string)

"AQwAtADQAMQA5AAwADEAfQAAA==" back to "This is a string!"

I knew that for the encoding Encoding.Unicode.GetBytes(string) was used. My fist idea was, to read two values, calculate the byte value and convert them back to a unicode string. Is there any better solution? Thanks.

2
  • I noticed that "==" padding. Is there any chance it is a base64 encoding related? Commented Aug 21, 2014 at 16:48
  • I noticed that too. I already checked that with the same could you suggested. But dose not work. I will check that tomorrow again. Thanks. Commented Aug 21, 2014 at 17:26

1 Answer 1

1

Just throwing this out there in case data came in with base64 encoded.

byte[] binaryData;
try {
      binaryData = 
         System.Convert.FromBase64String(base64String);
}
catch (System.ArgumentNullException) {
      //handling error
}

string myString = Encoding.Unicode.GetString(binaryData);

give it a try.

read more: http://msdn.microsoft.com/en-us/library/system.convert.frombase64string(v=vs.110).aspx

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

3 Comments

I tried that already but sadly the result is not the original input.
just another quick thought. maybe It's utf8 or utf32 and not unicode?
I did a huge mistake... I used Encoding.UTF8.GetString instead of Encoding.Unicode.GetString. Thanks!

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.