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

发布于 2024-12-02 20:50:23 字数 306 浏览 0 评论 0原文

可以说在 C++ 中我得到了这样的代码..

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

所以在 C# 中它会像..

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

无法将 byte[] 类型转换为 uint[]

如何将 C++ 对数组执行的内存对齐操作转换为 C#?

Lets say In C++ I got code like this..

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

So in C# it would be like..

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

Cannot convert type byte[] to uint[]

How do I convert this memory aligning thing C++ does to arrays to C#?

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

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

发布评论

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

评论(7

不打扰别人 2024-12-09 20:50:23

好吧,接近的方法是使用 Buffer.BlockCopy< /code>

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

请注意,BlockCopy 的最后一个参数始终是要复制的字节数,无论您要复制的类型如何。

在 C# 中,您不能将 byte 数组视为 uint 数组(至少在安全代码中不行;我不知道在不安全代码中如何) - 但是 < code>Buffer.BlockCopy 会将 byte 数组的内容喷射到 uint 数组中...结果根据字节序确定系统。就我个人而言,我喜欢这种方法 - 当您转移到具有不同内存布局的系统时,它会使代码很容易出错。我更喜欢在我的协议中明确说明。但希望它能在这种情况下对您有所帮助。

Well, something close would be to use 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.

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. 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.

回梦 2024-12-09 20:50:23

如果您愿意转向黑暗面,您可以拥有蛋糕(避免分配)并吃掉它(避免迭代)。

查看我对相关问题的回答,其中我演示了如何将 float[] 转换为 byte[],反之亦然:将 float[] 转换为 byte[] 的最快方法是什么?

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[]?

兲鉂ぱ嘚淚 2024-12-09 20:50:23

正如乔恩提到的,缓冲区。 BlockCopy 可以很好地复制它。

但是,如果这是一个互操作场景,并且您想要直接以 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++
}

我个人更喜欢制作副本,但如果您需要避免实际复制数据,这确实允许您工作(在不安全的上下文中)。

As Jon mentioned, Buffer.BlockCopy will work well for copying this.

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:

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).

三生一梦 2024-12-09 20:50:23

我使用了 BitConverter.ToUInt32() - https://learn.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);

看来为我工作得很好。

I used BitConverter.ToUInt32() - https://learn.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.

太阳男子 2024-12-09 20:50:23

您可以使用 Buffer.BlockCopy。而不是 Array.Copy, BlockCopy 执行字节级复制,而不检查数组类型是否完全兼容。

就像这样:

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

You can use Buffer.BlockCopy. Rather than Array.Copy, BlockCopy does a byte-level copy without checking the array types are fully compatible.

Like so:

uint[] array = new uint[bytes.Length/4];
Buffer.BlockCopy(bytes, 0, array, 0, bytes.Length);
末蓝 2024-12-09 20:50:23

晚了很多年,但也许这会对其他人有所帮助。 (这也是一种新的方式。)

我们可以使用 .Net Core 2.1 或更高版本 中的新 Span 进行 C++ 风格的重新解释转换(某种程度上)。没有堆内存分配,因此速度非常快。

Span<byte> byteArray = MemoryMarshal.AsBytes<uint>(uIntArray);
// with span we can get a byte, set a byte, iterate, and more.
byte someByte = byteSpan[2]; 
byteSpan[2] = 33;

如果需要 byte[],如问题中所述,则可以更进一步执行上述操作。 (这将分配内存并进行复制,但仍然很快。)

byte[] byteArray = MemoryMarshal.AsBytes<uint>(uIntArray).ToArray();

Years late but maybe this will help someone else. (It's also kind of new.)

We can do a c++ style reinterpret cast (sort of) using the new Span<> in .Net Core 2.1 or later. There are no heap memory allocations so it is very fast.

Span<byte> byteArray = MemoryMarshal.AsBytes<uint>(uIntArray);
// with span we can get a byte, set a byte, iterate, and more.
byte someByte = byteSpan[2]; 
byteSpan[2] = 33;

If byte[] is needed, as stated in the question, then the above can be taken one step further. (This would allocate memory and copy but it is still fast.)

byte[] byteArray = MemoryMarshal.AsBytes<uint>(uIntArray).ToArray();
想你只要分分秒秒 2024-12-09 20:50:23

循环遍历所有数组项并对每个数组项调用 Convert.ToUint32()。这里:

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

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

Loop over all array items and call Convert.ToUint32() on each of them.Here:

 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.
http://msdn.microsoft.com/en-us/library/469cwstk.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文