在C#.net 4中读取/写入字节数组 [英] Reading from/Writing to Byte Arrays in C# .net 4
本文介绍了在C#.net 4中读取/写入字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问候过花,
我喜欢内存映射文件的灵活性,因为您可以读取/写入任何值类型。
是否存在一种无需复制字节数组即可对字节数组执行相同操作的方法内存映射缓冲区?
I love the flexibility of memory mapped files in that you can read/write any value type.
Is there a way to do the same with byte arrays without having to copy them into for e.g. a memory map buffers ?
问候
推荐答案
BitConverter
类可在基本数据类型和字节数组之间进行转换。
You can use the BitConverter
class to convert between base data types and byte arrays.
您可以直接从数组中读取值:
You can read values directly from the array:
int value = BitConverter.ToInt32(data, pos);
要写入数据,请将其转换为字节数组,然后将其复制到数据中:
To write data you convert it to a byte array, and copy it into the data:
BitConverter.GetBytes(value).CopyTo(data, pos);
这篇关于在C#.net 4中读取/写入字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文