如何将字节数组转换为 UInt32 数组?
可以说在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
好吧,接近的方法是使用
Buffer.BlockCopy< /code>
:
请注意,
BlockCopy
的最后一个参数始终是要复制的字节数,无论您要复制的类型如何。在 C# 中,您不能将
byte
数组视为uint
数组(至少在安全代码中不行;我不知道在不安全代码中如何) - 但是 < code>Buffer.BlockCopy 会将byte
数组的内容喷射到uint
数组中...结果根据字节序确定系统。就我个人而言,我不喜欢这种方法 - 当您转移到具有不同内存布局的系统时,它会使代码很容易出错。我更喜欢在我的协议中明确说明。但希望它能在这种情况下对您有所帮助。Well, something close would be to use
Buffer.BlockCopy
: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 auint
array in C# (at least not in safe code; I don't know about in unsafe code) - butBuffer.BlockCopy
will splat the contents of thebyte
array into theuint
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.如果您愿意转向黑暗面,您可以拥有蛋糕(避免分配)并吃掉它(避免迭代)。
查看我对相关问题的回答,其中我演示了如何将 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[]?
正如乔恩提到的,缓冲区。 BlockCopy 可以很好地复制它。
但是,如果这是一个互操作场景,并且您想要直接以
uint[]
形式访问字节数组,那么您能做的最接近 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: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).
我使用了 BitConverter.ToUInt32() - https://learn.microsoft.com/en-us/dotnet/api/system.bitconverter.touint32?view=netcore-3.1
看来为我工作得很好。
I used BitConverter.ToUInt32() - https://learn.microsoft.com/en-us/dotnet/api/system.bitconverter.touint32?view=netcore-3.1
It seems to work fine for me.
您可以使用
Buffer.BlockCopy
。而不是Array.Copy
,BlockCopy
执行字节级复制,而不检查数组类型是否完全兼容。就像这样:
You can use
Buffer.BlockCopy
. Rather thanArray.Copy
,BlockCopy
does a byte-level copy without checking the array types are fully compatible.Like so:
晚了很多年,但也许这会对其他人有所帮助。 (这也是一种新的方式。)
我们可以使用 .Net Core 2.1 或更高版本 中的新 Span 进行 C++ 风格的重新解释转换(某种程度上)。没有堆内存分配,因此速度非常快。
如果需要 byte[],如问题中所述,则可以更进一步执行上述操作。 (这将分配内存并进行复制,但仍然很快。)
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.
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.)
循环遍历所有数组项并对每个数组项调用 Convert.ToUint32()。这里:
这是来自 MSDN 的官方链接。
http://msdn.microsoft.com/en-us/library/469cwstk.aspx
Loop over all array items and call Convert.ToUint32() on each of them.Here:
Here is an official link from MSDN.
http://msdn.microsoft.com/en-us/library/469cwstk.aspx