1、背景
在讀取byte數組的場景(例如:讀取文件、網絡傳輸數據)中,特別是網絡傳輸的場景中,非常有可能接收了不完整的byte數組,在將byte數組轉換時,因字符的缺失/增多,轉為亂碼。如下圖所示
2、具體實現
在C#中有Decoder可供使用,其內部有換存(buffer),可以現將非完整的byte存儲起來,等到后新的數據后,再拼接起來,形成完成的內容。具體代碼及說明如下:
// See https://aka.ms/new-console-template for more information
using System.ComponentModel;
using System.Text;//初始化的字符串
string helloStr = "你好Ha";
Console.WriteLine(helloStr);byte[] helloArray=Encoding.UTF8.GetBytes(helloStr);
Console.WriteLine("你好Ha對應的byte數組內容(十六進制):" + BitConverter.ToString(helloArray));ArraySegment<byte> helloSegment= new ArraySegment<byte>(helloArray,0,4);
Console.WriteLine("若網絡傳輸中先收到了前4個:"+BitConverter.ToString(helloSegment.ToArray()));
Console.WriteLine("轉換為string會發現結果是不對的:"+Encoding.UTF8.GetString(helloSegment));Console.WriteLine("下面是使用GetDecoder方法進行處理");
Decoder dncoder = Encoding.UTF8.GetDecoder();int charCount= dncoder.GetCharCount(helloSegment, false);
Console.WriteLine("還是同樣的前4個,判斷收到了幾個【完整】的字符:"+charCount);Console.WriteLine("將前3位,加載到halfCharArray數組中");
char[] halfCharArray = new char[charCount];
dncoder.GetChars(helloSegment.ToArray(), 0, helloSegment.Count, halfCharArray, 0);Console.WriteLine("解析完的字符是:"+ new string(halfCharArray));ArraySegment<byte> otherHalfStr = new ArraySegment<byte>(helloArray, 4, 4);
Console.WriteLine("若網絡傳輸中先收到了后4個:" + BitConverter.ToString(otherHalfStr.ToArray()));//繼續轉換
//若能判斷,本次接受的數據為最終的數據,則GetCharCount的第二個參數,應當設置為true,(強制清空Buffer,避免后續使用時影響后續的解析)
int otherCharCount = dncoder.GetCharCount(otherHalfStr, true);
Console.WriteLine("緩存+新接受的字符數量:" + otherCharCount);
char[] ohterHalfCharArray = new char[otherCharCount];
dncoder.GetChars(otherHalfStr.ToArray(), 0, otherHalfStr.Count, ohterHalfCharArray, 0);
Console.WriteLine("解析完的字符是:" + new string(ohterHalfCharArray));Console.Read();
以上代碼運行后的結果,如圖所示:
3、參考
官網的參考資料