C#的byte[]和AS3中的ByteArray都是字节数组.但是明显的AS3的ByteArray更加好用一些.因为在ByteArray当中有一个position属性,可以读取相应的字节后,自动指向下一个没有读取的字节的index.这样你永远不用自己再建一个index来手动的处理这件事情了.当然,ByteArray还有其他的一些方法和属性,是byte[]没有的.我这里强调,并非贬低C#,只是在这一块,需要做一些多余的事情,显得相当的繁琐.为此我封装了一个类库,核心类 BytesDecode如下:

//=====================================================================////Allrightreserved//filename:BytesDecode//description:////createbyUserat2016/8/129:57:15//=====================================================================usingSystem;usingSystem.Runtime.InteropServices;namespaceBytesLib.com{///<summary>///解析字节流///</summary>internalsealedclassBytesDecode{///<summary>///解析基础值类型///</summary>///<typeparamname="T">值类型</typeparam>///<paramname="bytes">字节流数组</param>///<paramname="index">开始Index</param>///<returns></returns>publicTgetStructValue<T>(byte[]bytes,refintindex)whereT:struct{returnthis.getValue<T>(bytes,refindex);}///<summary>///解析基础值类型数组///</summary>///<typeparamname="T">基础类型</typeparam>///<paramname="bytes">字节流数组</param>///<paramname="index">开始Index</param>///<paramname="len">数组长度</param>///<returns></returns>publicT[]getStructValus<T>(byte[]bytes,refintindex,intlen)whereT:struct{returnthis.getValues<T>(bytes,refindex,len);}///<summary>///基础值类型2维数组解析///</summary>///<typeparamname="T">基础类型</typeparam>///<paramname="bytes">字节流数组</param>///<paramname="index">开始Index</param>///<paramname="lenD1">一维Length</param>///<paramname="lenD2">二维Length</param>///<returns></returns>publicT[,]getStructValus2D<T>(byte[]bytes,refintindex,intlenD1,intlenD2)whereT:struct{returnthis.getValues2D<T>(bytes,refindex,lenD1,lenD2);}#region///<summary>///基础值类型解析///</summary>///<typeparamname="T">值类型</typeparam>///<paramname="bytes">字节流数组</param>///<paramname="index">开始Index</param>///<returns></returns>privateTgetValue<T>(byte[]bytes,refintindex)whereT:struct{Tbc=default(T);switch(typeof(T).Name.ToLower()){case"uint16":{bc=(T)Convert.ChangeType(BitConverter.ToUInt16(bytes,index),typeof(T));index+=Marshal.SizeOf(typeof(T));}break;case"int16":{bc=(T)Convert.ChangeType(BitConverter.ToInt16(bytes,index),typeof(T));index+=Marshal.SizeOf(typeof(T));}break;case"bool":case"boolean":{bc=(T)Convert.ChangeType(BitConverter.ToBoolean(bytes,index),typeof(T));index+=Marshal.SizeOf(typeof(T));}break;case"int64":{bc=(T)Convert.ChangeType(BitConverter.ToInt64(bytes,index),typeof(T));index+=Marshal.SizeOf(typeof(T));}break;case"uint64":{bc=(T)Convert.ChangeType(BitConverter.ToUInt64(bytes,index),typeof(T));index+=Marshal.SizeOf(typeof(T));}break;case"byte":{bc=(T)Convert.ChangeType(bytes[index],typeof(T));index+=Marshal.SizeOf(typeof(T));}break;case"int32":{bc=(T)Convert.ChangeType(BitConverter.ToInt32(bytes,index),typeof(T));index+=Marshal.SizeOf(typeof(T));}break;case"uint32":{bc=(T)Convert.ChangeType(BitConverter.ToUInt32(bytes,index),typeof(T));index+=Marshal.SizeOf(typeof(T));}break;}returnbc;}///<summary>///基础值类型数组解析///</summary>///<typeparamname="T">基础类型</typeparam>///<paramname="bytes">字节流数组</param>///<paramname="index">开始Index</param>///<paramname="len">数组长度</param>///<returns></returns>privateT[]getValues<T>(byte[]bytes,refintindex,intlen)whereT:struct{T[]bc=newT[len];inti=0;switch(typeof(T).Name.ToLower()){case"uint16":{while(i<len){bc[i]=(T)Convert.ChangeType(BitConverter.ToUInt16(bytes,index),typeof(T));index+=Marshal.SizeOf(typeof(T));i+=1;}}break;case"int16":{while(i<len){bc[i]=(T)Convert.ChangeType(BitConverter.ToInt16(bytes,index),typeof(T));index+=Marshal.SizeOf(typeof(T));i+=1;}}break;case"bool":case"boolean":{while(i<len){bc[i]=(T)Convert.ChangeType(BitConverter.ToBoolean(bytes,index),typeof(T));index+=Marshal.SizeOf(typeof(T));i+=1;}}break;case"int64":{while(i<len){bc[i]=(T)Convert.ChangeType(BitConverter.ToInt64(bytes,index),typeof(T));index+=Marshal.SizeOf(typeof(T));i+=1;}}break;case"uint64":{while(i<len){bc[i]=(T)Convert.ChangeType(BitConverter.ToUInt64(bytes,index),typeof(T));index+=Marshal.SizeOf(typeof(T));i+=1;}}break;case"byte":{while(i<len){bc[i]=(T)Convert.ChangeType(bytes[index],typeof(T));index+=Marshal.SizeOf(typeof(T));i+=1;}}break;case"int32":{while(i<len){bc[i]=(T)Convert.ChangeType(BitConverter.ToInt32(bytes,index),typeof(T));index+=Marshal.SizeOf(typeof(T));i+=1;}}break;case"uint32":{while(i<len){bc[i]=(T)Convert.ChangeType(BitConverter.ToUInt32(bytes,index),typeof(T));index+=Marshal.SizeOf(typeof(T));i+=1;}}break;case"char":{Buffer.BlockCopy(bytes,index,bc,0,len);index+=Marshal.SizeOf(bc);}break;}returnbc;}///<summary>///基础值类型2维数组解析///</summary>///<typeparamname="T">基础类型</typeparam>///<paramname="bytes">字节流数组</param>///<paramname="index">开始Index</param>///<paramname="lenD1">一维Length</param>///<paramname="lenD2">二维Length</param>///<returns></returns>privateT[,]getValues2D<T>(byte[]bytes,refintindex,intlenD1,intlenD2)whereT:struct{T[,]bc=newT[lenD1,lenD2];inti=0;switch(typeof(T).Name.ToLower()){case"uint16":{while(i<lenD1){for(intj=0;j<lenD2;j+=1){bc[i,j]=(T)Convert.ChangeType(BitConverter.ToUInt16(bytes,index),typeof(T));index+=Marshal.SizeOf(typeof(T));}i+=1;}}break;case"int16":{while(i<lenD1){for(intj=0;j<lenD2;j+=1){bc[i,j]=(T)Convert.ChangeType(BitConverter.ToInt16(bytes,index),typeof(T));index+=Marshal.SizeOf(typeof(T));}i+=1;}}break;case"bool":case"boolean":{while(i<lenD1){for(intj=0;j<lenD2;j+=1){bc[i,j]=(T)Convert.ChangeType(BitConverter.ToBoolean(bytes,index),typeof(T));index+=Marshal.SizeOf(typeof(T));}i+=1;}}break;case"int64":{while(i<lenD1){for(intj=0;j<lenD2;j+=1){bc[i,j]=(T)Convert.ChangeType(BitConverter.ToInt64(bytes,index),typeof(T));index+=Marshal.SizeOf(typeof(T));}i+=1;}}break;case"uint64":{while(i<lenD1){for(intj=0;j<lenD2;j+=1){bc[i,j]=(T)Convert.ChangeType(BitConverter.ToUInt64(bytes,index),typeof(T));index+=Marshal.SizeOf(typeof(T));}i+=1;}}break;case"byte":{while(i<lenD1){for(intj=0;j<lenD2;j+=1){bc[i,j]=(T)Convert.ChangeType(bytes[index],typeof(T));index+=Marshal.SizeOf(typeof(T));}i+=1;}}break;case"int32":{while(i<lenD1){for(intj=0;j<lenD2;j+=1){bc[i,j]=(T)Convert.ChangeType(BitConverter.ToInt32(bytes,index),typeof(T));index+=Marshal.SizeOf(typeof(T));}i+=1;}}break;case"uint32":{while(i<lenD1){for(intj=0;j<lenD2;j+=1){bc[i,j]=(T)Convert.ChangeType(BitConverter.ToUInt32(bytes,index),typeof(T));index+=Marshal.SizeOf(typeof(T));}i+=1;}}break;case"char":{Buffer.BlockCopy(bytes,index,bc,0,lenD1*lenD2);index+=Marshal.SizeOf(bc);}break;}returnbc;}#endregion}}

本类可以解析: 基础数据 , 基础数据一维数组 , 基础数据二维数组


在此额外奉上 基础数组2byte[]

//将基元数据类型数组转换成字节数组publicstaticbyte[]ToBytes<T>(T[]array)whereT:struct{if(array==null){thrownewArgumentNullException("array");}varbytes=newbyte[Buffer.ByteLength(array)];Buffer.BlockCopy(array,0,bytes,0,bytes.Length);returnbytes;}publicstaticbyte[]ToBytes<T>(T[,]array)whereT:struct{if(array==null){thrownewArgumentNullException("array");}varbytes=newbyte[Buffer.ByteLength(array)];Buffer.BlockCopy(array,0,bytes,0,bytes.Length);returnbytes;}

补充说明:

这个包是为Unity开发的.当然要知道它支持FrameWork的版本号了. 在PlayerSettings当中.关于如何使用FrameWork2.0来编译类库,自己Google