Is it possible to specify big/little endian for every field when using binary.Read() to decode a byte stream into a struct?

huangapple go评论88阅读模式
英文:

Is it possible to specify big/little endian for every field when using binary.Read() to decode a byte stream into a struct?

问题

当解码字节时,binary.Read()要求您指定该操作的预期字节顺序。binary.Read()还允许您传入一个结构体,但据我所知,它使用相同的字节顺序将字节流解码到结构体中的每个字段中。

当编码的整数字节顺序为小端,但编码的字符串和浮点数字节顺序为大端时,这是不方便的。

是否可以在每个字段的基础上指定解码字节流到结构体时使用的字节顺序?

英文:

When decoding bytes, binary.Read() requires you to specify the expected byte order of that operation. binary.Read() also allows you to pass in a struct, but AFAIK, it uses the same byte order to decode the byte stream into every field in the struct.

This is inconvenient when the byte order of encoded integers is in little endian but encoded strings and floats are in big endian.

Is it possible to specify on a per-field basis, which byte-order to use when decoding a stream of bytes into a struct?

答案1

得分: 1

不,看起来不是这样。

Read方法会完成解析需要读取的内容的所有工作...然后所有的实际读取方法都是这样的:

d.order.....

所以基本上,它们直接使用了你指定的ByteOrder,并没有尝试(通过结构标签或其他方式)让你能够按字段指定它。

很不幸...但我闻到了有人可以开发一个很棒的包,并与社区共享的机会 Is it possible to specify big/little endian for every field when using binary.Read() to decode a byte stream into a struct?

英文:

No, it doesn't look like it.

The Read method goes through all of the work of deciphering what it needs to read .. then all of the actual read methods have this:

d.order.....

So basically, they use the ByteOrder you've specified directly .. and make no attempt (via struct tags or anything else) to let you specify it on a per-field basis.

Unfortunate .. but I smell an opportunity for someone to come along and make a neat package that can be shared with the community Is it possible to specify big/little endian for every field when using binary.Read() to decode a byte stream into a struct?

huangapple
  • 本文由 发表于 2015年3月23日 06:16:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/29200499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定