.NET System.IO.BinaryWriter写入的原始字节是否可以被其他平台读取? [英] Are the raw bytes written by .NET System.IO.BinaryWriter readable by other platforms?

查看:89
本文介绍了.NET System.IO.BinaryWriter写入的原始字节是否可以被其他平台读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用System.IO.BinaryWriter手动将大数据块写入二进制文件.我之所以选择它,是因为与各种其他序列化和改进方法相比,它具有更高的性能.反序列化(我目前正在使用System.IO.BinaryReader反序列化).

I am manually writing a large data block into a binary file with 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).

我可能需要使用其他编程语言(如Java和/或Rust)中的序列化格式.他们是否能够理解System.IO.BinaryWriter编写的原始二进制文件并以类似于.NETs'System.IO.BinaryReader'的方式读取它?

I may need to use the serialized formats in other programming languages like Java and/or 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'?

(我假设新平台(Java/Rust)将对原始二进制文件的编写顺序具有隐式的了解.)

(I am assuming that the new plaforms (Java/Rust) will have implicit knowledge of the specific order in which the raw binary was written.)

我知道协议缓冲区在这种情况下应该是用于序列化/反序列化的高性能和语言不可知的框架,但是: (1)我使用的是F#,它与受歧视的工会斗争 (2)编写我自己的自定义序列化程序实际上并没有付出太多努力,因为我的类型不太复杂

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

推荐答案

这取决于您使用BinaryWriter编写的类型.

It depends on the types you write with the BinaryWriter.

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

这篇关于.NET System.IO.BinaryWriter写入的原始字节是否可以被其他平台读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆