简体   繁体   English

.NET System.IO.BinaryWriter写入的原始字节是否可以被其他平台读取?

[英]Are the raw bytes written by .NET System.IO.BinaryWriter readable by other platforms?

Background 背景

I am manually writing a large data block into a binary file with System.IO.BinaryWriter . 我正在使用System.IO.BinaryWriter手动将大数据块写入二进制文件。 I have chosen this due to the improved performance compared to a wide variety of other means of serialization & deserialization (I am currently deserializing with System.IO.BinaryReader ). 我之所以选择它,是因为与各种其他序列化和反序列化方法相比,性能有所改善(我目前正在使用System.IO.BinaryReader反序列化)。

Question

I may need to use the serialized formats in other programming languages like Java and/or Rust . 我可能需要在Java和/或Rust等其他编程语言中使用序列化格式。 Would they be able to understand the raw binary written by System.IO.BinaryWriter and read it in a similar manner to .NETs 'System.IO.BinaryReader'? 他们是否能够理解System.IO.BinaryWriter编写的原始二进制文件并以类似于.NETs'System.IO.BinaryReader'的方式读取它?

(I am assuming that the new plaforms (Java/Rust) will have implicit knowledge of the specific order in which the raw binary was written.) (我假设新平台(Java / Rust)将对原始二进制文件的写入特定顺序具有隐式知识。)

Side Info 边信息

I am aware that protocol buffers is meant to be a performant and language agnostic framework for serializing/deserializing in this scenario but: (1) I am using F# and it struggles with the discriminated unions (2) It wasn't really that much effort to write my own custom serializer as my types aren't too complex 我知道协议缓冲区在这种情况下本来是用于序列化/反序列化的高性能和语言不可知的框架,但是:(1)我正在使用F#,并且它与被区分的联合不兼容(2)确实没有那么多工作写我自己的自定义序列化程序,因为我的类型不太复杂

It depends on the types you write with the BinaryWriter . 这取决于您使用BinaryWriter编写的类型。

  • byte , sbyte and byte[] : no problem. bytesbytebyte[] :没问题。
  • (U)IntXX : matter of endianness. (U)IntXX :字节序的问题。 The .NET BinaryWriter dumps these types in little endian format. .NET BinaryWriter以小字节序格式转储这些类型。
  • float and double : If both systems use the same IEEE 754 standard, and both systems use the same endianness, then it is no problem. floatdouble :如果两个系统使用相同的IEEE 754标准,并且两个系统使用相同的字节序,则没有问题。
  • decimal : This is a .NET-specific type, similar to Currency but uses different format. decimal :这是.NET特定的类型,类似于Currency但使用不同的格式。 Use carefully. 小心使用。
  • char and char[] : Uses the current Encoding of the BinaryWriter . charchar[] :使用BinaryWriter的当前Encoding Use the same encoding on both sides and everything is alright. 双方使用相同的编码,一切正常。
  • string : The length of the string is encoded in the so-called 7 bit-encoded int format (1 byte for up to 127 chars, etc), and uses the current encoding. stringstring的长度以所谓的7位编码的int格式(1个字节,最多127个字符,等等)进行编码, 使用当前的编码。 To make things compatible maybe you should dump character arrays with manually dumped length information. 为了使事情兼容,您应该使用手动转储的长度信息转储字符数组。

Yes, you can. 是的你可以。

bool     --> 0 | 1
sbyte    --> x
byte[]   --> xxxxxx
char[]   --> encoding.getbytes(char[])
byte     --> x
char     --> 
decimal  --> decimal.GetBytes(), 16 bytes, should see the System.Decimal class code
double   --> 8 bytes, should see the System.Double class code
short    --> 2 bytes, <lsb><msb>
int      --> 4 byets, <lsb>xx<msb>
long     --> 8 bytes, <lsb>xxxxxx<msb>
float    --> 4 bytes, should see the System.Single class code
string   --> 7 bit encoded length (variable size) + encoding.GetBytes(), see 7 bit encoding method below
ushort   --> same as short
uint     --> same as int
ulong    --> same as long

For numeric types, data is written in Little Endian Format 对于数字类型,数据以Little Endian格式写入

protected void Write7BitEncodedInt(int value)
{
    uint num = (uint) value;
    while (num >= 0x80)
    {
        this.Write((byte) (num | 0x80));
        num = num >> 7;
    }
    this.Write((byte) num);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用System.IO.BinaryWriter写入文件 - Writing File using System.IO.BinaryWriter 将long编码为VLQ字节数组并将其写入System.IO.BinaryWriter - Encoding a long to a VLQ byte array and writing it to System.IO.BinaryWriter 使用System.IO.BinaryWriter编写字符串与char数组的区别 - Difference in writing string vs. char array with System.IO.BinaryWriter 如何在C#中将System.IO.BinaryWriter与模板/通用类型数组一起使用? - How to use System.IO.BinaryWriter in C# with array of template/generic type? 如何使用BinaryWriter获取写入字节的长度? - How to get the length of written bytes with BinaryWriter? 我如何修改使用 binarywriter 写入的内存流中的一小部分字节 - how can i modify a small section of bytes in a memory stream, that was written to using binarywriter System.Net.ProtocolViolationException:要写入流的字节超过了指定的Content-Length字节大小 - System.Net.ProtocolViolationException:Bytes to be written to the stream exceed the Content-Length bytes size specified 使用Binarywriter写空字节 - Write empty bytes using binarywriter 如何从 .NET 的 BinaryWriter 序列化的四个字节中读取单精度浮点数 - How to read a single-precision floating-point number from four bytes serialized by .NET's BinaryWriter System.IO.Abstraction.TestingHelpers - 在不同平台上进行测试 - System.IO.Abstraction.TestingHelpers - testing on the different platforms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM