2014-04-09 186 views
0

我得到一個Soap響應,其中包含一個base64字符串。我使用的XDocument得到元素的值和這樣的功能來讀取它解碼從Windows Phone的base64字符串

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; 
     } 

但結果值與在線解碼器的結果略有不同。

我錯過了這裏的東西?

+3

哪裏是你的base64字符串 this is a dummy encoded base64 string?你如何解碼它?你如何使用XDocument? BinaryReader的用途是什麼? –

+0

如何將base64編碼與UTF8編碼聯繫起來? –

+0

L.B我更新了我的問題。哈姆雷特Hakobyan你能解釋一下你的意思嗎? – Billatron

回答

3

我不知道你正在嘗試與BinaryReader做,但這裏是我做的就是從你的base64數據

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

感謝您的回覆。這似乎是有效的,如果data64很簡單,但如果我試圖把一些更復雜的東西返回一些不同的字符,有些顯示像那樣的 ..爲什麼會發生這種情況。 – Billatron

+0

@Billatron如何在不知道原始base64字符串的情況下回答問題? –

+0

用新數據字符串更新了原始問題。 – Billatron