从windows phone解码base64字符串

时间:2014-04-09 19:00:52

标签: c# utf8-decode

我收到一个包含base64字符串的Soap响应。我正在使用XDocument获取元素的值和这样的函数来读取它

 public void main()
    {
  //****UPDATE
  string data64 = "";
  data64 = removeNewLinesFromString(data64);
  content = data64.ToCharArray();

  byte[] binaryData = Convert.FromBase64CharArray(content, 0, content.Length);
  Stream stream  = new MemoryStream(binaryData);
  BinaryReader reader = new BinaryReader(stream,Encoding.UTF8);                                        
  string object64 = SoapSerializable.ReadUTF(reader);
  }

这是readUTF函数

   public static String ReadUTF(BinaryReader reader)
        {
            // read the following string's length in bytes
            int length = Helpers.FlipInt32(reader.ReadInt32());

            // read the string's data bytes
            byte[] utfString = reader.ReadBytes(length);

            // get the string by interpreting the read data as UTF-8
            return System.Text.Encoding.UTF8.GetString(utfString, 0, utfString.Length);
        }

和我的FlipInt32功能

public static Int32 FlipInt32(Int32 value)
        {
            Int32 a = (value >> 24) & 0xFF;
            Int32 b = (value >> 16) & 0xFF;
            Int32 c = (value >> 8) & 0xFF;
            Int32 d = (value >> 0) & 0xFF;
            return (((((d << 8) | c) << 8) | b) << 8) | a;
        }

但结果值与在线解码器给出的结果略有不同。

我在这里遗失了什么?

1 个答案:

答案 0 :(得分:3)

我不确定你要用BinaryReader做什么但是这就是我要做的事情 来自base64数据的this is a dummy encoded base64 string

string data64 = "dGhpcyBpcyBhIGR1bW15IGVuY29kZWQgYmFzZTY0IHN0cmluZy4=";
var buf = Convert.FromBase64String(data64);
var str = Encoding.UTF8.GetString(buf);