[英]Writeable byte stream style functionality in C#
我需要通过将int
和float
/ double
数据写入其中来构建字节流。 如何在C#中轻松完成此任务? 我知道获取float变量的原始字节的方法,但是C#是否已经有了一个可以轻松利用的字节流写入系统?
从字节数组读取浮点值:
uint floatBytes = .. // read 4 float bytes from byte[] array
float floatVal = *((float*)&floatBytes);
将float值写入bytearray:
float floatVal = ... // read a float from the float[] array
uint floatBytes = *((uint*)&floatVal);
C#是否已经有一个我可以轻松利用的字节流写入系统?
.NET库为此有一对流装饰器BinaryWriter和BinaryReader。
var reader = new BinaryReader(someStream);
float f1 = reader.ReadSingle(); // Single == float
double d1 = reader.ReadDouble();
string s1 = reader.ReadString(); // the Writer issues a length-prefix.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.