当前位置:首页 > 后端 > C# > C#数据类型转换类

C#数据类型转换类

DChen3年前 (2021-04-05)C#1.59 K0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace BaseTools
{   
    public class ConvertData
    {
        #region
 
        /*
         <summary>
         根据四字节返回数组
         </summary>
         <param name="value"></param>
         <returns></returns>
        */
        public static Byte[] GetInt32Bytes(Int32 value)
        {
            Byte[] bytes = System.bitConverter.GetBytes(value).Reverse().ToArray();
 
            return bytes;
        }
 
        /*
         <summary>
         根据四字节返回数组
         </summary>
         <param name="value"></param>
         <returns></returns>
        */
        public static Byte[] GetInt32Bytes(UInt32 value)
        {
            Byte[] bytes = System.BitConverter.GetBytes(value).Reverse().ToArray();
 
            return bytes;
        }
        
        /*
         <summary>
         根据双字节返回数组
         </summary>
         <param name="value"></param>
         <returns></returns>
        */
        public static Byte[] GetInt16Bytes(Int16 value)
        {
            Byte[] bytes = System.BitConverter.GetBytes(value).Reverse().ToArray();
 
            return bytes;
        }
 
        /*
         <summary>
         根据双字节返回数组
         </summary>
         <param name="value"></param>
         <returns></returns>
        */
        public static Byte[] GetInt16Bytes(UInt16 value)
        {
            Byte[] bytes = System.BitConverter.GetBytes(value).Reverse().ToArray();
 
            return bytes;
        }
 
        /*
         <summary>
         根据提供的数组返回2字节的Int16
         </summary>
         <param name="data">数组</param>
         <param name="startIndex">数组开始的索引</param>
         <returns>2字节的整型数</returns>
        */
        public static Int16 GetBytesToInt16(Byte[] data, int startIndex)
        {
            Int16 value = 0;
 
            byte[] converterData = new byte[2];
 
            converterData[0] = data[startIndex + 1];
            converterData[1] = data[startIndex];
 
            value = System.BitConverter.ToInt16(converterData, 0);
 
            return value;
        }
 
        /*
        /// <summary>
        /// 根据提供的数组返回2字节的UInt16
        /// </summary>
        /// <param name="data"></param>
        /// <param name="startIndex"></param>
        /// <returns></returns>
        */
        public static UInt16 GetBytesToUInt16(Byte[] data, int startIndex)
        {
            UInt16 value = 0;
 
            byte[] converterData = new byte[2];
 
            converterData[0] = data[startIndex + 1];
            converterData[1] = data[startIndex];
 
            value = System.BitConverter.ToUInt16(converterData, 0);
 
            return value;
        }
 
        /*
        /// <summary>
        /// 根据提供的数组返回2字节的整型数,并增加索引
        /// </summary>
        /// <param name="data">数组</param>
        /// <param name="startIndex">数组开始的索引</param>
        /// <returns>2字节的整型数</returns>
        */
        public static Int16 GetBytesToInt16(byte[] data, ref int startIndex)
        {
            int index = startIndex;
            startIndex = startIndex + 2;
            return GetBytesToInt16(data, index);
        }
 
        /*
        /// <summary>
        /// 根据提供的数组返回2字节的整型数,并增加索引
        /// </summary>
        /// <param name="data">数组</param>
        /// <param name="startIndex">数组开始的索引</param>
        /// <returns>2字节的整型数</returns>
        */
        public static UInt16 GetBytesToUInt16(byte[] data, ref int startIndex)
        {
            int index = startIndex;
            startIndex = startIndex + 2;
            return GetBytesToUInt16(data, index);
        }
 
        /*
        /// <summary>
        /// 根据提供的数组返回4字节的UInt32
        /// </summary>
        /// <param name="data">数组</param>
        /// <param name="startIndex">数组开始的索引</param>
        /// <returns>4字节的整型数</returns>
        */
        public static UInt32 GetBytesToUInt32(Byte[] data, int startIndex)
        {
            UInt32 value = 0;
 
            byte[] converterData = new byte[4];
 
            converterData[0] = data[startIndex + 3];
            converterData[1] = data[startIndex + 2];
            converterData[2] = data[startIndex + 1];
            converterData[3] = data[startIndex];
 
            value = BitConverter.ToUInt32(converterData, 0);
 
            return value;
        }
 
        /*
        /// <summary>
        /// 根据提供的数组返回4字节的整型数
        /// </summary>
        /// <param name="data">数组</param>
        /// <param name="startIndex">数组开始的索引</param>
        /// <returns>4字节的整型数</returns>
        */
        public static Int32 GetBytesToInt32(Byte[] data, int startIndex)
        {
            Int32 value = 0;
 
            byte[] converterData = new byte[4];
 
            converterData[0] = data[startIndex + 3];
            converterData[1] = data[startIndex + 2];
            converterData[2] = data[startIndex + 1];
            converterData[3] = data[startIndex];
 
            value = BitConverter.ToInt32(converterData, 0);
 
            return value;
        }
 
        /*
        /// <summary>
        /// 根据提供的数组放回4字节的UInt32,并增加索引
        /// </summary>
        /// <param name="data">数组</param>
        /// <param name="startIndex">数组开始的索引</param>
        /// <returns>4字节的整型数</returns>
        */
        public static UInt32 GetBytesToUInt32(Byte[] data, ref int startIndex)
        {
            int index = startIndex;
            startIndex = startIndex + 4;
            return GetBytesToUInt32(data, index);
        }
 
        /*
        /// <summary>
        /// 根据提供的数组放回4字节的整型数,并增加索引
        /// </summary>
        /// <param name="data">数组</param>
        /// <param name="startIndex">数组开始的索引</param>
        /// <returns>4字节的整型数</returns>
        */
        public static Int32 GetBytesToInt32(Byte[] data, ref int startIndex)
        {
            int index = startIndex;
            startIndex = startIndex + 4;
            return GetBytesToInt32(data, index);
        }
 
        /*
        /// <summary>
        /// 根据提供的数组,获取指定开始的索引和长度复制到新的数组中
        /// </summary>
        /// <param name="command">数组</param>
        /// <param name="startIndex">开始索引</param>
        /// <param name="len">长度</param>
        /// <returns>新的数组</returns>
        */
        public static byte[] GetByttes(byte[] command, int startIndex, int len)
        {
            byte[] data = new byte[len];
 
            Array.Copy(command, startIndex, data, 0, len);
 
            return data;
        }
 
        /*
        /// <summary>
        /// 根据提供的数组,获取指定开始的索引和长度,复制到新的数组中,并增加索引
        /// </summary>
        /// <param name="command">数组</param>
        /// <param name="startIndex">开始索引</param>
        /// <param name="len">长度</param>
        /// <returns>新的数组</returns>
        */
        public static byte[] GetByttes(byte[] command, ref int startIndex, int len)
        {
            int index = startIndex;
            startIndex += len;
            return GetByttes(command, index, len);
        }
 
        /*
        /// <summary>
        /// 从提供的数组中读取一个字节的数据
        /// </summary>
        /// <param name="command">数组</param>
        /// <param name="startIndex">索引</param>
        /// <returns>1字节数据</returns>
        */
        public static byte ReadByte(byte[] command, int startIndex)
        {
            return command[startIndex];
        }
 
        /*
        /// <summary>
        /// 从提供的数组中读取一个字节的数据,并增加索引
        /// </summary>
        /// <param name="command">数组</param>
        /// <param name="startIndex">索引</param>
        /// <returns>字节数据</returns>
        */
        public static byte ReadByte(byte[] command, ref int startIndex)
        {
            int index = startIndex;
            startIndex++;
            return ReadByte(command, index);
        }
 
        /*
        /// <summary>
        /// 根据提供的数组,反向读取数组,复制到新的数组
        /// </summary>
        /// <param name="command">数组</param>
        /// <param name="startIndex">索引</param>
        /// <param name="len">长度</param>
        /// <returns>新的数组</returns>
        */
        public static byte[] GetBytesAdjust(byte[] command, int startIndex, int len)
        {
            byte[] data = new byte[len];
 
            for (int i = 0; i < len; i++)
            {
                data[i] = command[startIndex + len - i - 1];
            }
 
            return data;
        }
 
        /*
        /// <summary>
        /// 根据提供的数组,反向读取数组,复制到新的数组,并增加索引
        /// </summary>
        /// <param name="command">数组</param>
        /// <param name="startIndex">索引</param>
        /// <param name="len">长度</param>
        /// <returns>新的数组</returns>
        */
        public static byte[] GetBytesAdjust(byte[] command, ref int startIndex, int len)
        {
            int index = startIndex;
            startIndex += len;
            return GetBytesAdjust(command, index, len);
        }
 
        /*
        /// <summary>
        /// 字节数组中的某一段转换成十六进制字符串 {0xFF,0xdf} = "FFDF"
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        */
        public static string GetBytesToHexStr(byte[] data, int startIndex, int len)
        {
            byte[] Data = GetByttes(data, startIndex, len);
 
            StringBuilder sb = new StringBuilder();
 
            foreach (byte b in Data)
            {
                sb.Append(b.ToString("X2"));
            }
 
            return sb.ToString();
        }
 
        /*
        /// <summary>
        /// 字节数组中的某一段转换成十六进制字符串 {0xFF,0xDF} = "FFDF",并增加索引
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        */
        public static string GetBytesToHexStr(byte[] data,ref int startIndex, int len)
        {
            int Index = startIndex;
 
            startIndex += len;
 
            return GetBytesToHexStr(data, Index, len);
        }
 
        /*
        /// <summary>
        /// 十六进制字符串中的某段字符转换成字节数组 "FFDF" = {0xFF,0xDF}
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        */
        public static byte[] GetHexStrBytes(string data, int startIndex, int len)
        {
            List<byte> byteBufs = new List<byte>();
 
            int EndIndex = startIndex + len;
 
            char aChar;
 
            byte aByte;
 
            if (data.Length < EndIndex) return byteBufs.ToArray();
 
            for (int i = startIndex; i < EndIndex; i++)
            {
                aChar = data[i];
                
                // 输出高4位
                if (aChar >= '0' && aChar <= '9')
                {
                    aByte = (byte)((aChar - '0') << 4);
                }
                else
                {
                    aByte = (byte)((aChar - 'A' + 10) << 4);
                }
 
                //末尾不足则补零
                if (++i < EndIndex)
                {
                    aChar = data[i];
 
                    // 输出低4位
                    if (aChar >= '0' && aChar <= '9')
                    {
                        aByte |= (byte)(aChar - '0');
                    }
                    else
                    {
                        aByte |= (byte)(aChar - 'A' + 10);
                    }
                }
 
                byteBufs.Add(aByte);                
            }
 
            return byteBufs.ToArray();      
        }
 
        /*
        /// <summary>
        /// 十六进制字符串中的某段字符转换成字节数组 "FFDF" = {0xFF,0xDF},并增加索引。
        /// </summary>
        */
        public static byte[] GetHexStrBytes(string data, ref int startIndex, int len)        
        {
            int index = startIndex;
 
            startIndex += len;
 
            return GetHexStrBytes(data, index, len);
        }
 
        /*
        /// <summary>
        /// 根据dateTime类型(long)返回数组
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        */
        public static Byte[] GetDateTimeBytes(DateTime dt)
        {
            return BitConverter.GetBytes(dt.Ticks);
        }
 
        /*
        /// <summary>
        /// 根据提供的数组返回DateTime型数
        /// </summary>
        /// <param name="data">数组</param>
        /// <param name="startIndex">数组开始的索引</param>
        /// <returns>DateTime型数</returns>
        */
        public static DateTime GetBytesToDateTime(byte[] data, int startIndex)
        {
            if (data != null)
            {
                long ticks = BitConverter.ToInt64(data, startIndex);
 
                if (ticks < DateTime.MaxValue.Ticks && ticks > DateTime.MinValue.Ticks)
                {
                    DateTime dt = new DateTime(ticks);
                    return dt;
                }
            }
            return new DateTime();
        }
 
        /*
        /// <summary>
        /// 根据提供的数组返回DateTime型数,并增加索引
        /// </summary>
        /// <param name="data">数组</param>
        /// <param name="startIndex">数组开始的索引</param>
        /// <returns>DateTime日期型</returns>
        */
        public static DateTime GetBytesToDateTime(byte[] data, ref int StartIndex)
        {
            int index = StartIndex;
 
            StartIndex = StartIndex + 8;
 
            return GetBytesToDateTime(data, index);
        }
 
        /*
        /// <summary>
        /// 根据字符串返回编码后的字节数组
        /// </summary>
        /// <param name="Context">字符内容</param>
        /// <param name="Encode">编码类型</param>
        /// <returns></returns>
        */
        public static Byte[] GetStringBytes(String Context, String Encode)
        {
            byte[] RecordByte = Encoding.GetEncoding(Encode).GetBytes(Context);
 
            return RecordByte;
        }
 
        /*
        /// <summary>
        /// 根据字符串返回编码字节数组,数组前部存放长度信息。(长度占四字节)
        /// </summary>
        /// <param name="Context">字符内容</param>
        /// <param name="Encode">编码类型</param>
        /// <returns></returns>
        */
        public static Byte[] GetStringLenBytes(String Context, String Encode)
        {
            byte[] RecordByte = Encoding.GetEncoding(Encode).GetBytes(Context);
 
            List<byte> Buf = new List<byte>();
 
            //Buf.AddRange(GetInt16Bytes(RecordByte.Length));
            Buf.AddRange(GetInt32Bytes(RecordByte.Length));
 
            Buf.AddRange(RecordByte);
 
            RecordByte = Buf.ToArray();
 
            return RecordByte;
        }
 
        public static Byte[] GetStringLenBytes16(String Context, String Encode)
        {
            byte[] RecordByte = Encoding.GetEncoding(Encode).GetBytes(Context);
 
            List<byte> Buf = new List<byte>();
 
            //Buf.AddRange(GetInt16Bytes(RecordByte.Length));
            Buf.AddRange(GetInt16Bytes(Convert.ToInt16(RecordByte.Length)));
 
            Buf.AddRange(RecordByte);
 
            RecordByte = Buf.ToArray();
 
            return RecordByte;
        }
 
        /*
        /// <summary>
        /// 根据提供的数组返回字符串
        /// </summary>
        /// <param name="data">数组</param>
        /// <param name="startIndex">数组开始的索引</param>
        /// <returns>DateTime型数</returns>
        */
        public static String GetBytesToLenString(byte[] data, int StartIndex, String Encode)
        {
            String Ret = "";
 
            int Index = StartIndex;
 
            int Len = GetBytesToInt32(data, ref Index);
 
            Ret = GetBytesToString(data, Index, Len, Encode);
 
            return Ret;
        }
 
        /*
        /// <summary>
        /// 根据提供的数组返回字符串
        /// </summary>
        /// <param name="data">数组</param>
        /// <param name="startIndex">数组开始的索引</param>
        /// <returns>DateTime型数</returns>
        */
        public static String GetBytesToLenString(byte[] data, ref int StartIndex, String Encode)
        {
            String Ret = "";
 
            int Index = StartIndex;
 
            int Len = GetBytesToInt32(data, ref Index);
 
            Ret = GetBytesToString(data, ref Index, Len, Encode);
 
            StartIndex = Index;
 
            return Ret;
        }
 
        public static String GetBytesToLenString16(byte[] data, ref int StartIndex, String Encode)
        {
            String Ret = "";
 
            int Index = StartIndex;
 
            int Len = GetBytesToInt16(data, ref Index);
 
            Ret = GetBytesToString(data, ref Index, Len, Encode);
 
            StartIndex = Index;
 
            return Ret;
        }
 
        /*
        /// <summary>
        /// 根据提供的数组返回字符串
        /// Encode: 
        ///     "ascii":  返回PAnsi型字符串(遇\0结束)
        ///     "ASCII-PZ": 返回PAnsi型字符串(过滤掉里面的\0)
        /// </summary>
        /// <param name="data">数组</param>
        /// <param name="startIndex">数组开始的索引</param>
        /// <returns>DateTime型数</returns>
        */
        public static String GetBytesToString(byte[] data, int StartIndex, int len, String Encode)
        {
            byte[] RecordByte = GetByttes(data, StartIndex, len);
 
            if (Encode == "ASCII")
            {
                List<byte> Buf = new List<byte>();
 
                foreach (byte aChar in RecordByte)
                {
                    if (aChar != 0x00)
                        Buf.Add(aChar);
                    else
                        break;
                }
 
                return Encoding.GetEncoding(Encode).GetString(Buf.ToArray());
            }
 
            if (Encode == "ASCII-PZ")
            {
                List<byte> Buf = new List<byte>();
 
                foreach (byte aChar in RecordByte)
                {
                    if (aChar != 0x00)
                        Buf.Add(aChar);                 
                }
 
                return Encoding.GetEncoding("ASCII").GetString(Buf.ToArray());
            }
          
            return Encoding.GetEncoding(Encode).GetString(RecordByte);
        }
 
        /*
        /// <summary>
        /// 根据提供的数组返回字符串
        /// </summary>
        /// <param name="data">数组</param>
        /// <param name="startIndex">数组开始的索引</param>
        /// <returns>DateTime型数</returns>
        */
        public static String GetBytesToString(byte[] data, ref int StartIndex, int Len, String Encode)
        {
            int index = StartIndex;
 
            StartIndex += Len;
 
            return GetBytesToString(data, index, Len, Encode);
        }
 
        /*
        /// <summary>
        /// 根据浮点型返回数组
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        */
        public static Byte[] GetDoubleBytes(double value)
        {
            Byte[] bytes = System.BitConverter.GetBytes(value).Reverse().ToArray();
 
            return bytes;
        }
 
        /*
        /// <summary>
        /// 根据数组返回浮点型
        /// </summary>
        /// <param name="data">数组</param>
        /// <param name="startIndex">数组开始的索引</param>
        /// <returns>浮点型数</returns>
        */
        public static double GetBytesToDouble(Byte[] data, int startIndex)
        {
            double value = 0;
            byte[] converterData = new byte[sizeof(double)];
 
            int I = 0;
            int J = converterData.Length;
 
            for (I = 0; I < converterData.Length; I++)
            {
                converterData[I] = data[startIndex + (--J)];
            }
 
            value = BitConverter.ToDouble(converterData, 0);
 
            return value;
        }
 
        /*
        /// <summary>
        /// 根据数组返回浮点型,并增加索引
        /// </summary>
        /// <param name="data">数组</param>
        /// <param name="startIndex">数组开始的索引</param>
        /// <returns>浮点型数</returns>
        */
        public static double GetBytesToDouble(Byte[] data, ref int StartIndex)
        {
            int index = StartIndex;
 
            StartIndex += sizeof(double);
 
            return GetBytesToDouble(data, index);
        }
 
        /*
        /// <summary>
        /// 根据n值返回相应长度字节的数组(MQTT协议的剩余长度的编码算法)
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        */
        public static byte[] intToByte(int n)
        {
            List<byte> list = new List<byte>();
            while (n > 0)
            {
                int byteOf = n % 128;
                n /= 128;
                if (n > 0)
                {
                    byteOf |= 128;
                    list.Add((byte)byteOf);
                }
                else
                {
                    list.Add((byte)byteOf);
                }
            }
            return list.ToArray();
        }
 
        /*
        /// <summary>
        /// 根据n值返回相应长度(MQTT协议的剩余长度的编码算法)
        /// </summary>
        /// <param name="data"></param>
        /// <param name="StartIndex"></param>
        /// <returns></returns>
        */
        public static Int32 BytesToInt32(byte[] data, ref int StartIndex)
        {
            int multiplier = 1;
            int value = 0;
            byte encodedByte;
            do
            {
                encodedByte = ReadByte(data, ref StartIndex);
                value += (encodedByte & 127) * multiplier;
                multiplier *= 128;
                if (multiplier > 128 * 128 * 128)
                {
                    value = 0;
                    break;
                }
            } while ((encodedByte & 128) != 0);
            return value;
        }
 
        /*
        /// <summary>
        /// 根据相应长度字节的数组返回int值(即剩余长度的十进制值)
        /// </summary>
        /// <param name="byteList"></param>
        /// <returns></returns>
        */
        public static int byteToInt(byte[] byteList)
        {
            int multiplier = 1;
            int value = 0;
            for (int i = 0; i < byteList.Length; i++)
            {
                value += (byteList[i] & 127) * multiplier;
                multiplier *= 128;
            }
            return value;
        }
 
        /*
        /// <summary>
        /// 判断数组中,某一位是不是零?未测试。需修正。
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        */
        /*
        public static bool IsBitZero(byte[] data,int BitIndex)
        {
            bool Ret = false;         
            Byte aByte = 0;
            int Bitposi = BitIndex % 8;
            int ByteIndex = BitIndex/8;
            if (data.Length <= 0) return Ret;
            if (data.Length >= ByteIndex)
            { 
                aByte = (Byte)(data[ByteIndex] & (1<<BitPosi));
                if (aByte==0)
                {
                    Ret = false;
                }
            }
            return Ret;
        }*/
 
 
 
 
        #endregion
    }
}


取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

版权声明:本文由“憨小猪”发布,如需转载请注明出处。

本文链接:https://www.phper.red/post/42.html

标签: C#
返回列表

没有更早的文章了...

没有最新的文章了...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。