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

文章介绍了在C#中如何使用BitConverter类将常见数据类型如int、long、short等数组转换为byte数组,以及如何将byte数组转换回这些数据类型,以便在MySQL数据库中以二进制形式存储和读取数组数据。

【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;
}
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

椒颜皮皮虾྅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值