溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C# 從字節(jié)數組讀取基礎數據

發(fā)布時間:2020-07-31 12:52:21 來源:網絡 閱讀:1160 作者:Aonaufly 欄目:開發(fā)技術

C#的byte[]和AS3中的ByteArray都是字節(jié)數組.但是明顯的AS3的ByteArray更加好用一些.因為在ByteArray當中有一個position屬性,可以讀取相應的字節(jié)后,自動指向下一個沒有讀取的字節(jié)的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
{
    /// <summary>
    /// 解析字節(jié)流
    /// </summary>
    internal sealed class BytesDecode
    {
        /// <summary>
        /// 解析基礎值類型
        /// </summary>
        /// <typeparam name="T">值類型</typeparam>
        /// <param name="bytes">字節(jié)流數組</param>
        /// <param name="index">開始Index</param>
        /// <returns></returns>
        public T getStructValue<T>(byte[] bytes, ref int index) where T : struct
        {
            return this.getValue<T>(bytes, ref index);
        }
        /// <summary>
        /// 解析基礎值類型數組
        /// </summary>
        /// <typeparam name="T">基礎類型</typeparam>
        /// <param name="bytes">字節(jié)流數組</param>
        /// <param name="index">開始Index</param>
        /// <param name="len">數組長度</param>
        /// <returns></returns>
        public T[] getStructValus<T>(byte[] bytes, ref int index, int len) where T : struct
        {
            return this.getValues<T>(bytes, ref index, len);
        }
        /// <summary>
        /// 基礎值類型2維數組解析
        /// </summary>
        /// <typeparam name="T">基礎類型</typeparam>
        /// <param name="bytes">字節(jié)流數組</param>
        /// <param name="index">開始Index</param>
        /// <param name="lenD1">一維Length</param>
        /// <param name="lenD2">二維Length</param>
        /// <returns></returns>
        public T[,] getStructValus2D<T>(byte[] bytes, ref int index, int lenD1, int lenD2) where T : struct
        {
            return this.getValues2D<T>(bytes, ref index, lenD1, lenD2);
        }
        #region
        /// <summary>
        /// 基礎值類型解析
        /// </summary>
        /// <typeparam name="T">值類型</typeparam>
        /// <param name="bytes">字節(jié)流數組</param>
        /// <param name="index">開始Index</param>
        /// <returns></returns>
        private T getValue<T>(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;
        }
        /// <summary>
        /// 基礎值類型數組解析
        /// </summary>
        /// <typeparam name="T">基礎類型</typeparam>
        /// <param name="bytes">字節(jié)流數組</param>
        /// <param name="index">開始Index</param>
        /// <param name="len">數組長度</param>
        /// <returns></returns>
        private T[] getValues<T>(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;
        }
        /// <summary>
        /// 基礎值類型2維數組解析
        /// </summary>
        /// <typeparam name="T">基礎類型</typeparam>
        /// <param name="bytes">字節(jié)流數組</param>
        /// <param name="index">開始Index</param>
        /// <param name="lenD1">一維Length</param>
        /// <param name="lenD2">二維Length</param>
        /// <returns></returns>
        private T[,] getValues2D<T>(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[]

     //將基元數據類型數組轉換成字節(jié)數組    
     public static byte[] ToBytes<T>(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>(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開發(fā)的.當然要知道它支持FrameWork的版本號了. 在PlayerSettings當中.關于如何使用FrameWork2.0來編譯類庫,自己Google

C# 從字節(jié)數組讀取基礎數據

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI