如何在 C# 中直接将托管流读入 uint 数组?
我在流中有一些数据,实际上是 uint 格式。 System.IO.Stream只允许我将内容读取到字节数组中,但我不想读取字节然后将它们转换为长度为字节数组的1/4的uint数组。我想直接将其读入uint数组的内存中,这样可以节省时间。
如果它是在非托管 C++ 中,那就很简单了。只需将指针发送到 uint 数组即可。但在 C# 中,这似乎并不那么简单。我试图生成一个与另一个 uint 数组具有相同内存地址的字节数组,然后将字节数组作为 Stream.Read() 的参数发送,以便当 Stream.Read() 写入字节数组时,内容只是也在 uint 数组中。但我查看了 Marshall 类,到目前为止还没有找到解决这个问题的方法。有什么想法吗?
I have some data in a stream which is actually in the format of uint. System.IO.Stream only allows me to read the content out to a byte array, but I don't want to read the bytes and then convert them into a uint array with 1/4 the length of the byte array. I want to read it directly into the memory of the uint array so that it saves time.
If it's in unmanaged C++ it's just so easy. Just send over the pointer to the uint array and it'd be done. But in C# it doesn't seem so straightforward. I'm trying to produce a byte array with the same memory address as another uint array and then send the byte array as the parameter of Stream.Read() so that when Stream.Read() writes to the byte array the content is just in the uint array as well. But I've looked at the Marshall class and have so far found no solution to this problem. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,某些流实现不支持位置和/或长度属性。
Note that some stream implementations don't support Position and/or Length properties.