天天看點

C# 基礎資料與Byte

基礎資料與Byte資料的轉換在Socket通訊中用的非常的多.我想任何Game都不大會希望直接用XML和Json字元串直接進行資料傳遞,而是在Client端和Server端對基礎資料進行解析.當然,如果你執行用"Encoding.UTF8.GetBytes"之類的話,我也沒有辦法.

C# 基礎資料與Byte

好了,進入正題:在C#中要進行基礎資料和Byte的轉換要用到:"BitConverter"類

這裡我用了一個執行個體:

BitConverToByte : 将基礎類型變成Bytes[]

BitBitConverterTest : 将Bytes[]解析成基礎類型

另外:簡易的模仿一下Socket通訊協定(注意:隻是簡易,真正用在Socket裡面,需要另外加協定号等等): 

標頭隻有一個 int32類型用于包體的length

包體:

    [

        int32 : 内容的長度

        string : 内容

    ]

    int32 , 這裡故意加的(免得成光棍内容)( 在實際Socket中 , 你可以表示年齡啥的!!!)

代碼如下:

BitConverToByte :

using System;     using System.Collections.Generic;     using System.Linq;     using System.Text;     using System.Threading.Tasks;     namespace BitBitConverterTest.ainy     {         /// <summary>         /// 将内容變成規則的二進制資料,以便發送         /// </summary>         public class BitConverToByte         {             private readonly string context;             private readonly Int32 mark;             public BitConverToByte( string context , Int32 mark )             {                 this.context = context;                 this.mark = mark;             }             /// <summary>             /// 傳回二進制數組             /// </summary>             /// <returns> int32長度 + context + mark</returns>             public byte[] GetContextBytes()             {                 // 内容的二進制                 byte[] myContext = Encoding.UTF8.GetBytes(this.context);                 // 标示的二進制( 測試BitConverter )                 byte[] myMark = BitConverter.GetBytes(this.mark);                 // 關于内容長度 myContext + myMark                 byte[] myBytesLenght = BitConverter.GetBytes(myContext.Length + myMark.Length);                 // 關于内容的長度                 byte[] myContextLength = BitConverter.GetBytes(myContext.Length);                 byte[] reslut = new byte[myContext.Length + myContextLength.Length + myMark.Length + myBytesLenght.Length];                 myBytesLenght.CopyTo(reslut, 0);                 myContextLength.CopyTo(reslut, myBytesLenght.Length);                 myContext.CopyTo(reslut, myBytesLenght.Length + myContextLength.Length);                 myMark.CopyTo(reslut, myBytesLenght.Length + myContext.Length + myContextLength.Length);                 return reslut;             }         }     }      

BitBitConverterTest :

using System;     using System.Collections.Generic;     using System.Linq;     using System.Text;     using System.Threading.Tasks;     namespace BitBitConverterTest.ainy     {         /// <summary>         /// 将二進制轉化為内容         /// </summary>         public class BitConverToContext         {             //整個二進制資料             private readonly byte[] target;             //文本二進制(String)資料             public string Context { get ; private set; }             //标記資料             public Int32 Mark { get; private set; }             //文本 "I Love U"的二進制資料             public byte[] ContextBytes { get; private set; }             public BitConverToContext( byte[] target )             {                 this.target = target;                 this.AnalysisFromBytes();             }             /// <summary>             /// 解析Byte[]             /// int + [string的length + string] + int             /// </summary>             private void AnalysisFromBytes() {                 // 得到文本的總長度 : ( 文本長度 + Context.Length + Mark.Length )                 Int32 contextLength = BitConverter.ToInt32(this.target, 0);                 // 得到文本的長度                 Int32 len = BitConverter.ToInt32(this.target, 4);                 // 擷取文本                 this.Context = BitConverter.ToString(this.target, 8, len);                 // 擷取後面的Mark                 this.Mark = BitConverter.ToInt32(this.target, 8 + len);                 this.ContextBytes = this.target.Skip(8).Take(len).ToArray();             }         }     }      

測試:

using System;     using System.Collections.Generic;     using System.Linq;     using System.Text;     using System.Threading.Tasks;     using BitBitConverterTest.ainy;     namespace BitBitConverterTest     {         class Program         {             static void Main(string[] args)             {                 BitConverToByte bitConverToByte = new BitConverToByte("I Love U", 1314);                 BitConverToContext bitConverToContext = new BitConverToContext(bitConverToByte.GetContextBytes());                 Console.WriteLine("内容二進制:{0} , 内容:{1} , 标記:{2}", bitConverToContext.Context, Encoding.UTF8.GetString(bitConverToContext.ContextBytes), bitConverToContext.Mark);                 Console.ReadKey();             }         }     }      

結果:

C# 基礎資料與Byte