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?
    – TLJ
    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

3
  • I tried that already but sadly the result is not the original input. Commented Aug 21, 2014 at 17:21
  • just another quick thought. maybe It's utf8 or utf32 and not unicode?
    – TLJ
    Commented Aug 21, 2014 at 21:40
  • I did a huge mistake... I used Encoding.UTF8.GetString instead of Encoding.Unicode.GetString. Thanks! Commented Aug 22, 2014 at 6:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.