C# 从 uint[] 转换为 byte[]

发布于 2024-12-14 04:32:40 字数 346 浏览 0 评论 0原文

这可能是一个简单的方法,但我似乎找不到一种简单的方法来做到这一点。我需要将 84 uint 数组保存到 SQL 数据库的 BINARY 字段中。因此,我在 C# ASP.NET 项目中使用以下几行:

//This is what I have
uint[] uintArray;

//I need to convert from uint[] to byte[]
byte[] byteArray = ???

cmd.Parameters.Add("@myBindaryData", SqlDbType.Binary).Value = byteArray;

那么如何从 uint[] 转换为 byte[]?

This might be a simple one, but I can't seem to find an easy way to do it. I need to save an array of 84 uint's into an SQL database's BINARY field. So I'm using the following lines in my C# ASP.NET project:

//This is what I have
uint[] uintArray;

//I need to convert from uint[] to byte[]
byte[] byteArray = ???

cmd.Parameters.Add("@myBindaryData", SqlDbType.Binary).Value = byteArray;

So how do you convert from uint[] to byte[]?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

绮烟 2024-12-21 04:32:40

怎么样:

byte[] byteArray = uintArray.SelectMany(BitConverter.GetBytes).ToArray();

这会以小端格式做你想做的事......

How about:

byte[] byteArray = uintArray.SelectMany(BitConverter.GetBytes).ToArray();

This'll do what you want, in little-endian format...

把人绕傻吧 2024-12-21 04:32:40

您可以使用 System.Buffer.BlockCopy 来执行此操作:

byte[] byteArray = new byte[uintArray.Length * 4];
Buffer.BlockCopy(uintArray, 0, byteArray, 0, uintArray.Length * 4];

http:// /msdn.microsoft.com/en-us/library/system.buffer.blockcopy.aspx

这比使用 for 循环或某些类似的构造要高效得多。它直接将字节从第一个数组复制到第二个数组。

要转换回来,只需反向执行相同的操作即可。

You can use System.Buffer.BlockCopy to do this:

byte[] byteArray = new byte[uintArray.Length * 4];
Buffer.BlockCopy(uintArray, 0, byteArray, 0, uintArray.Length * 4];

http://msdn.microsoft.com/en-us/library/system.buffer.blockcopy.aspx

This will be much more efficient than using a for loop or some similar construct. It directly copies the bytes from the first array to the second.

To convert back just do the same thing in reverse.

陌上芳菲 2024-12-21 04:32:40

没有内置的转换函数可以执行此操作。由于数组的工作方式,需要分配一个全新的数组并填充其值。您可能只需要自己编写即可。您可以使用 System.BitConverter.GetBytes(uint) 函数完成一些工作,然后将结果值复制到最终的byte[] 中。

这是一个将以小端格式进行转换的函数:

    private static byte[] ConvertUInt32ArrayToByteArray(uint[] value)
    {
        const int bytesPerUInt32 = 4;
        byte[] result = new byte[value.Length * bytesPerUInt32];
        for (int index = 0; index < value.Length; index++)
        {
            byte[] partialResult = System.BitConverter.GetBytes(value[index]);
            for (int indexTwo = 0; indexTwo < partialResult.Length; indexTwo++)
                result[index * bytesPerUInt32 + indexTwo] = partialResult[indexTwo];
        }
        return result;
    }

There is no built-in conversion function to do this. Because of the way arrays work, a whole new array will need to be allocated and its values filled-in. You will probably just have to write that yourself. You can use the System.BitConverter.GetBytes(uint) function to do some of the work, and then copy the resulting values into the final byte[].

Here's a function that will do the conversion in little-endian format:

    private static byte[] ConvertUInt32ArrayToByteArray(uint[] value)
    {
        const int bytesPerUInt32 = 4;
        byte[] result = new byte[value.Length * bytesPerUInt32];
        for (int index = 0; index < value.Length; index++)
        {
            byte[] partialResult = System.BitConverter.GetBytes(value[index]);
            for (int indexTwo = 0; indexTwo < partialResult.Length; indexTwo++)
                result[index * bytesPerUInt32 + indexTwo] = partialResult[indexTwo];
        }
        return result;
    }
就是爱搞怪 2024-12-21 04:32:40
byte[] byteArray = Array.ConvertAll<uint, byte>(
    uintArray,
    new Converter<uint, byte>(
        delegate(uint u) { return (byte)u; }
    ));

听取@liho1eye 的建议,确保您的 uint 真正适合字节,否则您将丢失数据。

byte[] byteArray = Array.ConvertAll<uint, byte>(
    uintArray,
    new Converter<uint, byte>(
        delegate(uint u) { return (byte)u; }
    ));

Heed advice from @liho1eye, make sure your uints really fit into bytes, otherwise you're losing data.

内心激荡 2024-12-21 04:32:40

如果您需要每个 uint 的所有位,则必须创建一个适当大小的 byte[] 并将每个 uint 复制到它表示的四个字节中。

像这样的东西应该有效:

uint[] uintArray;

//I need to convert from uint[] to byte[]
byte[] byteArray = new byte[uintArray.Length * sizeof(uint)];
for (int i = 0; i < uintArray.Length; i++)
{
    byte[] barray = System.BitConverter.GetBytes(uintArray[i]);
    for (int j = 0; j < barray.Length; j++)
    {
          byteArray[i * sizeof(uint) + j] = barray[j];
    }
}
cmd.Parameters.Add("@myBindaryData", SqlDbType.Binary).Value = byteArray;

If you need all the bits from each uint, you're gonna to have to make an appropriately sized byte[] and copy each uint into the four bytes it represents.

Something like this ought to work:

uint[] uintArray;

//I need to convert from uint[] to byte[]
byte[] byteArray = new byte[uintArray.Length * sizeof(uint)];
for (int i = 0; i < uintArray.Length; i++)
{
    byte[] barray = System.BitConverter.GetBytes(uintArray[i]);
    for (int j = 0; j < barray.Length; j++)
    {
          byteArray[i * sizeof(uint) + j] = barray[j];
    }
}
cmd.Parameters.Add("@myBindaryData", SqlDbType.Binary).Value = byteArray;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文