简体   繁体   English

如何将字节数组转换为 UInt32 数组?

[英]How do I convert byte array to UInt32 array?

Lets say In C++ I got code like this..让我们说在 C++ 中我得到了这样的代码..

void * target
uint32 * decPacket = (uint32 *)target;

So in C# it would be like..所以在 C# 中它会像..

byte[] target;
UInt32[] decPacket = (UInt32[])target;

Cannot convert type byte[] to uint[]无法将类型 byte[] 转换为 uint[]

How do I convert this memory aligning thing C++ does to arrays to C#?我如何将 C++ 的这种内存对齐方式转换为数组到 C#?

Well, something close would be to use Buffer.BlockCopy :好吧,接近于使用Buffer.BlockCopy

uint[] decoded = new uint[target.Length / 4];
Buffer.BlockCopy(target, 0, decoded, 0, target.Length);

Note that the final argument to BlockCopy is always the number of bytes to copy, regardless of the types you're copying.请注意, BlockCopy的最后一个参数始终是要复制的字节数,无论您要复制的类型如何。

You can't just treat a byte array as a uint array in C# (at least not in safe code; I don't know about in unsafe code) - but Buffer.BlockCopy will splat the contents of the byte array into the uint array... leaving the results to be determined based on the endianness of the system.你不能在 C# 中将byte数组视为uint数组(至少不是在安全代码中;我不知道在不安全代码中) - 但Buffer.BlockCopy会将byte数组的内容放入uint数组中...让结果取决于系统的字节序。 Personally I'm not a fan of this approach - it leaves the code rather prone to errors when you move to a system with a different memory layout.就我个人而言,我喜欢这种方法 - 当您移动到具有不同内存布局的系统时,它会使代码很容易出错。 I prefer to be explicit in my protocol.我更喜欢在我的协议中明确。 Hopefully it'll help you in this case though.希望它会在这种情况下帮助你。

You can have the cake (avoid allocations) and eat it too (avoid iterations), if you're willing to move to the dark side.如果您愿意转向阴暗面,您可以拥有蛋糕(避免分配)并吃掉它(避免迭代)。

Check out my answer to a related question, in which I demonstrate how to convert float[] to byte[] and vice versa: What is the fastest way to convert a float[] to a byte[]?查看我对相关问题的回答,其中演示了如何将 float[] 转换为 byte[],反之亦然:将 float[] 转换为 byte[] 的最快方法是什么?

As Jon mentioned , Buffer.BlockCopy will work well for copying this. 正如 Jon 提到的,Buffer.BlockCopy 可以很好地复制它。

However, if this is an interop scenario, and you want to access the byte array directly as uint[] , the closest you can do is to the C++ approach would be to use unsafe code:但是,如果这是一个互操作场景,并且您想直接作为uint[]访问字节数组,那么您可以做的最接近 C++ 的方法是使用不安全代码:

byte[] target;
CallInteropMethod(ref target);

fixed(byte* t = target)
{
   uint* decPacket = (uint*)t;

   // You can use decPacket here the same way you do in C++
}

I personally prefer making the copy, but if you need to avoid actually copying the data, this does allow you to work (in an unsafe context).我个人更喜欢制作副本,但如果您需要避免实际复制数据,这确实允许您工作(在不安全的环境中)。

I used BitConverter.ToUInt32() - https://docs.microsoft.com/en-us/dotnet/api/system.bitconverter.touint32?view=netcore-3.1我使用了 BitConverter.ToUInt32() - https://docs.microsoft.com/en-us/dotnet/api/system.bitconverter.touint32?view=netcore-3.1

byte[] source = new byte[n];
UInt32 destination;

destination = BitConverter.ToUInt32(source, 0);

It seems to work fine for me.这对我来说似乎很好。

You can use Buffer.BlockCopy .您可以使用Buffer.BlockCopy Rather than Array.Copy , BlockCopy does a byte-level copy without checking the array types are fully compatible.而不是Array.CopyBlockCopy做一个字节级拷贝而不检查阵列类型是完全兼容。

Like so:像这样:

uint[] array = new uint[bytes.Length/4];
Buffer.BlockCopy(bytes, 0, array, 0, bytes.Length);

Loop over all array items and call Convert.ToUint32() on each of them.Here:循环遍历所有数组项并对每个项调用 Convert.ToUint32()。这里:

 Uint32[] res = new Uint32[target.Length];
 for(int i = 0;i <= target.Length;i++) 
 {
     res[i] = Convert.ToUint32(target[i]);
 }

Here is an official link from MSDN.这是来自 MSDN 的官方链接。 http://msdn.microsoft.com/en-us/library/469cwstk.aspx http://msdn.microsoft.com/en-us/library/469cwstk.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM