基础数据与Byte数据的转换在Socket通讯中用的非常的多.我想任何Game都不大会希望直接用XML和Json字符串直接进行数据传递,而是在Client端和Server端对基础数据进行解析.当然,如果你执行用"Encoding.UTF8.GetBytes"之类的话,我也没有办法.好了,进入正题:在C#中要进行基础数据和Byte的转换要用到:"BitConverter"类

这里我用了一个实例:

BitConverToByte : 将基础类型变成Bytes[]

BitBitConverterTest : 将Bytes[]解析成基础类型

另外:简易的模仿一下Socket通讯协议(注意:只是简易,真正用在Socket里面,需要另外加协议号等等):

包头只有一个 int32类型用于包体的length

包体:

[

int32 : 内容的长度

string : 内容

]

int32 , 这里故意加的(免得成光棍内容)( 在实际Socket中 , 你可以表示年龄啥的!!!)

代码如下:

BitConverToByte :

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceBitBitConverterTest.ainy{///<summary>///将内容变成规则的二进制数据,以便发送///</summary>publicclassBitConverToByte{privatereadonlystringcontext;privatereadonlyInt32mark;publicBitConverToByte(stringcontext,Int32mark){this.context=context;this.mark=mark;}///<summary>///返回二进制数组///</summary>///<returns>int32长度+context+mark</returns>publicbyte[]GetContextBytes(){//内容的二进制byte[]myContext=Encoding.UTF8.GetBytes(this.context);//标示的二进制(测试BitConverter)byte[]myMark=BitConverter.GetBytes(this.mark);//关于内容长度myContext+myMarkbyte[]myBytesLenght=BitConverter.GetBytes(myContext.Length+myMark.Length);//关于内容的长度byte[]myContextLength=BitConverter.GetBytes(myContext.Length);byte[]reslut=newbyte[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);returnreslut;}}}

BitBitConverterTest :

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceBitBitConverterTest.ainy{///<summary>///将二进制转化为内容///</summary>publicclassBitConverToContext{//整个二进制数据privatereadonlybyte[]target;//文本二进制(String)数据publicstringContext{get;privateset;}//标记数据publicInt32Mark{get;privateset;}//文本"ILoveU"的二进制数据publicbyte[]ContextBytes{get;privateset;}publicBitConverToContext(byte[]target){this.target=target;this.AnalysisFromBytes();}///<summary>///解析Byte[]///int+[string的length+string]+int///</summary>privatevoidAnalysisFromBytes(){//得到文本的总长度:(文本长度+Context.Length+Mark.Length)Int32contextLength=BitConverter.ToInt32(this.target,0);//得到文本的长度Int32len=BitConverter.ToInt32(this.target,4);//获取文本this.Context=BitConverter.ToString(this.target,8,len);//获取后面的Markthis.Mark=BitConverter.ToInt32(this.target,8+len);this.ContextBytes=this.target.Skip(8).Take(len).ToArray();}}}

测试:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingBitBitConverterTest.ainy;namespaceBitBitConverterTest{classProgram{staticvoidMain(string[]args){BitConverToBytebitConverToByte=newBitConverToByte("ILoveU",1314);BitConverToContextbitConverToContext=newBitConverToContext(bitConverToByte.GetContextBytes());Console.WriteLine("内容二进制:{0},内容:{1},标记:{2}",bitConverToContext.Context,Encoding.UTF8.GetString(bitConverToContext.ContextBytes),bitConverToContext.Mark);Console.ReadKey();}}}

结果:

附件:http://down.51cto.com/data/2367312