天天看点

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