【C#】常见数据类型数组与二进制数据数组byte[]转换

【C#】常见数据类型数组与二进制数据数组byte[]转换


  最近在研究在MySQL数据库中保存数组数据,查使用的方法是将数组转为二进制数据,以二进制数据的格式将数组保存到数据库中,这就涉及到了一个数组转二进制数据的方法,在C#中,主要使用byte格式的数据保存二进制,如果使用强制转化会出现问题,好在C#提供了专门转换的类 BitConverter,在该类下提供了常见数据与byte数据转换的方法。
  在本文章中,封装了常见的数据类型数组与Byte数组之间的互相转换,在使用是可以直接进行调用:

使用案例

short[] data = new short[] { 5, 6, 98, 34, 56, 77 };
byte[] by = data_to_byte<short>(data);
short[] data1 = byte_to_data<short>(by);
Console.WriteLine(data1.Length);
for (int ji = 0; ji < data1.Length; ji++) 
{ 
    Console.WriteLine(data1[ji]); 
}

常见数据类型转byte[]

/// <summary>
/// 将常见的数据类型数组转为二进制数组
/// </summary>
/// <typeparam name="T">数据类行</typeparam>
/// <param name="data">数据</param>
/// <returns>byte数组</returns>
public static byte[] data_to_byte<T>(T[] data)
{
    byte[] bydata;
    if (typeof(T) == typeof(int))
    {
        bydata = new byte[data.Length * 4];
        for (int i = 0; i < data.Length; i++)
        {
            int tem = Convert.ToInt32(data[i]);
            byte[] b = BitConverter.GetBytes(tem);
            for (int j = 0; j < 4; j++)
            {
                bydata[i * 4 + j] = b[j];
            }
        }
    }
    else if (typeof(T) == typeof(long))
    {
        bydata = new byte[data.Length * 8];
        for (int i = 0; i < data.Length; i++)
        {
            long tem = (long)Convert.ToInt64(data[i]);
            byte[] b = BitConverter.GetBytes(tem);
            for (int j = 0; j < 8; j++)
            {
                bydata[i * 8 + j] = b[j];
            }
        }
    }
    else if (typeof(T) == typeof(short))
    {
        bydata = new byte[data.Length * 2];
        for (int i = 0; i < data.Length; i++)
        {
            short tem = (short)Convert.ToInt16(data[i]);
            byte[] b = BitConverter.GetBytes(tem);
            for (int j = 0; j < 2; j++)
            {
                bydata[i * 2 + j] = b[j];
            }
        }
    }
    else if (typeof(T) == typeof(uint))
    {
        bydata = new byte[data.Length * 4];
        for (int i = 0; i < data.Length; i++)
        {
            uint tem = Convert.ToUInt32(data[i]);
            byte[] b = BitConverter.GetBytes(tem);
            for (int j = 0; j < 4; j++)
            {
                bydata[i * 4 + j] = b[j];
            }
        }
    }
    else if (typeof(T) == typeof(ulong))
    {
        bydata = new byte[data.Length * 8];
        for (int i = 0; i < data.Length; i++)
        {
            ulong tem = (ulong)Convert.ToUInt64(data[i]);
            byte[] b = BitConverter.GetBytes(tem);
            for (int j = 0; j < 8; j++)
            {
                bydata[i * 8 + j] = b[j];
            }
        }
    }
    else if (typeof(T) == typeof(ushort))
    {
        bydata = new byte[data.Length * 2];
        for (int i = 0; i < data.Length; i++)
        {
            ushort tem = (ushort)Convert.ToUInt16(data[i]);
            byte[] b = BitConverter.GetBytes(tem);
            for (int j = 0; j < 2; j++)
            {
                bydata[i * 2 + j] = b[j];
            }
        }
    }
    else if (typeof(T) == typeof(float))
    {
        bydata = new byte[data.Length * 4];
        for (int i = 0; i < data.Length; i++)
        {
            float tem = (float)Convert.ToDouble(data[i]);
            byte[] b = BitConverter.GetBytes(tem);
            for (int j = 0; j < 4; j++)
            {
                bydata[i * 4 + j] = b[j];
            }
        }
    }
    else if (typeof(T) == typeof(double))
    {
        bydata = new byte[data.Length * 8];
        for (int i = 0; i < data.Length; i++)
        {
            double tem = Convert.ToDouble(data[i]);
            byte[] b = BitConverter.GetBytes(tem);
            for (int j = 0; j < 8; j++)
            {
                bydata[i * 8 + j] = b[j];
            }
        }
    }
    else if (typeof(T) == typeof(char))
    {
        bydata = new byte[data.Length * 2];
        for (int i = 0; i < data.Length; i++)
        {
            char tem = Convert.ToChar(data[i]);
            byte[] b = BitConverter.GetBytes(tem);
            for (int j = 0; j < 2; j++)
            {
                bydata[i * 2 + j] = b[j];
            }
        }
    }
    else
    {
        bydata = new byte[1];
    }
    return bydata;
}

byte[]转指定数据类型

/// <summary>
/// 将byte数组转为指定的数据类型
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="bydata">输入数组</param>
/// <returns数组></returns>
public static T[] byte_to_data<T>(byte[] bydata)
{
    T[] data;
    if (typeof(T) == typeof(int))
    {
        data = new T[bydata.Length / 4];
        for (int i = 0; i < data.Length; i++)
        {
            byte[] b = new byte[4];
            for (int j = 0; j < 4; j++)
            {
                b[j] = bydata[i * 4 + j];
            }
            object tem = BitConverter.ToInt32(b);
            data[i] = (T)tem;
        }
    }
    else if (typeof(T) == typeof(long))
    {
        data = new T[bydata.Length / 8];
        for (int i = 0; i < data.Length; i++)
        {
            byte[] b = new byte[8];
            for (int j = 0; j < 8; j++)
            {
                b[j] = bydata[i * 8 + j];
            }
            object tem = BitConverter.ToInt64(b);
            data[i] = (T)tem;
        }
    }
    else if (typeof(T) == typeof(short))
    {
        data = new T[bydata.Length / 2];
        for (int i = 0; i < data.Length; i++)
        {
            byte[] b = new byte[2];
            for (int j = 0; j < 2; j++)
            {
                b[j] = bydata[i * 2 + j];
            }
            object tem = BitConverter.ToInt16(b);
            data[i] = (T)tem;
        }
    }
    else if (typeof(T) == typeof(uint))
    {
        data = new T[bydata.Length / 4];
        for (int i = 0; i < data.Length; i++)
        {
            byte[] b = new byte[4];
            for (int j = 0; j < 4; j++)
            {
                b[j] = bydata[i * 4 + j];
            }
            object tem = BitConverter.ToUInt32(b);
            data[i] = (T)tem;
        }
    }
    else if (typeof(T) == typeof(ulong))
    {
        data = new T[bydata.Length / 8];
        for (int i = 0; i < data.Length; i++)
        {
            byte[] b = new byte[8];
            for (int j = 0; j < 8; j++)
            {
                b[j] = bydata[i * 8 + j];
            }
            object tem = BitConverter.ToUInt64(b);
            data[i] = (T)tem;
        }
    }
    else if (typeof(T) == typeof(ushort))
    {
        data = new T[bydata.Length / 2];
        for (int i = 0; i < data.Length; i++)
        {
            byte[] b = new byte[2];
            for (int j = 0; j < 2; j++)
            {
                b[j] = bydata[i * 2 + j];
            }
            object tem = BitConverter.ToUInt16(b);
            data[i] = (T)tem;
        }
    }
    else if (typeof(T) == typeof(float))
    {
        data = new T[bydata.Length / 4];
        for (int i = 0; i < data.Length; i++)
        {
            byte[] b = new byte[4];
            for (int j = 0; j < 4; j++)
            {
                b[j] = bydata[i * 4 + j];
            }
            object tem = (float)BitConverter.ToDouble(b);
            data[i] = (T)tem;
        }
    }
    else if (typeof(T) == typeof(double))
    {
        data = new T[bydata.Length / 8];
        for (int i = 0; i < data.Length; i++)
        {
            byte[] b = new byte[8];
            for (int j = 0; j < 8; j++)
            {
                b[j] = bydata[i * 8 + j];
            }
            object tem = BitConverter.ToDouble(b);
            data[i] = (T)tem;
        }
    }
    else if (typeof(T) == typeof(char))
    {
        data = new T[bydata.Length / 2];
        for (int i = 0; i < data.Length; i++)
        {
            byte[] b = new byte[2];
            for (int j = 0; j < 2; j++)
            {
                b[j] = bydata[i * 2 + j];
            }
            object tem = BitConverter.ToChar(b);
            data[i] = (T)tem;
        }
    }
    else
    {
        data = new T[1];
    }
    return data;
}

猜你喜欢

转载自blog.csdn.net/grape_yan/article/details/130416079