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

//=====================================================================
//
//  All right reserved
//  filename : BytesDecode
//  description :
//
//  create by User at 2016/8/12 9:57:15
//=====================================================================
using System;
using System.Runtime.InteropServices;

namespace BytesLib.com
{
    /// 
    /// 解析字节流
    /// 
    internal sealed class BytesDecode
    {
        /// 
        /// 解析基础值类型
        /// 
        /// 值类型
        /// 字节流数组
        /// 开始Index
        /// 
        public T getStructValue(byte[] bytes, ref int index) where T : struct
        {
            return this.getValue(bytes, ref index);
        }
        /// 
        /// 解析基础值类型数组
        /// 
        /// 基础类型
        /// 字节流数组
        /// 开始Index
        /// 数组长度
        /// 
        public T[] getStructValus(byte[] bytes, ref int index, int len) where T : struct
        {
            return this.getValues(bytes, ref index, len);
        }
        /// 
        /// 基础值类型2维数组解析
        /// 
        /// 基础类型
        /// 字节流数组
        /// 开始Index
        /// 一维Length
        /// 二维Length
        /// 
        public T[,] getStructValus2D(byte[] bytes, ref int index, int lenD1, int lenD2) where T : struct
        {
            return this.getValues2D(bytes, ref index, lenD1, lenD2);
        }
        #region
        /// 
        /// 基础值类型解析
        /// 
        /// 值类型
        /// 字节流数组
        /// 开始Index
        /// 
        private T getValue(byte[] bytes, ref int index) where T : struct
        {
            T bc = 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;
            }
            return bc;
        }
        /// 
        /// 基础值类型数组解析
        /// 
        /// 基础类型
        /// 字节流数组
        /// 开始Index
        /// 数组长度
        /// 
        private T[] getValues(byte[] bytes, ref int index, int len) where T : struct
        {
            T[] bc = new T[len];
            int i = 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;
            }
            return bc;
        }
        /// 
        /// 基础值类型2维数组解析
        /// 
        /// 基础类型
        /// 字节流数组
        /// 开始Index
        /// 一维Length
        /// 二维Length
        /// 
        private T[,] getValues2D(byte[] bytes, ref int index, int lenD1, int lenD2) where T : struct
        {
            T[,] bc = new T[lenD1,lenD2];
            int i = 0;
            switch (typeof(T).Name.ToLower())
            {
                case "uint16":
                    {
                        while (i < lenD1)
                        {
                            for (int j = 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 (int j = 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 (int j = 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 (int j = 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 (int j = 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 (int j = 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 (int j = 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 (int j = 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;
            }
            return bc;
        }
        #endregion
    }
}

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


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

     //将基元数据类型数组转换成字节数组    
     public static byte[] ToBytes(T[] array) where T : struct
    {
        if (array == null)
        {
            throw new ArgumentNullException("array");
        }
        var bytes = new byte[Buffer.ByteLength(array)];
        Buffer.BlockCopy(array, 0, bytes, 0, bytes.Length);
        return bytes;
    }
    public static byte[] ToBytes(T[,] array) where T : struct
    {
        if (array == null)
        {
            throw new ArgumentNullException("array");
        }
        var bytes = new byte[Buffer.ByteLength(array)];
        Buffer.BlockCopy(array, 0, bytes, 0, bytes.Length);
        return bytes;
    }

补充说明:

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

C# 从字节数组读取基础数据_第1张图片