简体   繁体   English

C++:如何以独立于平台的方式读写多字节 integer 值?

[英]C++: How to read and write multi-byte integer values in a platform-independent way?

I'm developing a simple protocol that is used to read/write integer values from/to a buffer.我正在开发一个简单的协议,用于从/向缓冲区读取/写入 integer 值。 The vast majority of integers are below 128, but much larger values are possible, so I'm looking at some form of multi-byte encoding to store the values in a concise way.绝大多数整数都低于 128,但更大的值是可能的,所以我正在寻找某种形式的多字节编码来以简洁的方式存储值。

What is the simplest and fastest way to read/write multi-byte values in a platform-independent (ie byte order agnostic) way?以独立于平台(即字节顺序不可知)的方式读取/写入多字节值的最简单和最快的方法是什么?

XDR format might help you there. XDR格式可能会对您有所帮助。 If I had to summarize it in one sentence, it's a kind of binary UTF-8 for integers.如果非要用一句话来概括的话,那就是整数的一种二进制 UTF-8。

Edit: As mentioned in my comment below, I "know" XDR because I use several XDR-related functions in my office job.编辑:正如我在下面的评论中提到的,我“知道”XDR,因为我在办公室工作中使用了几个与 XDR 相关的功能。 Only after your comment I realized that the "packed XDR" format I use every day isn't even part of the official XDR docs, so I'll describe it seperately.只有在发表评论后,我才意识到我每天使用的“打包 XDR”格式甚至不是官方 XDR 文档的一部分,所以我将单独描述它。

The idea is thus:因此,这个想法是:

  • inspect most-significant bit of byte.检查字节的最高有效位。
    • If it is 0, that byte is the value.如果为 0,则该字节值。
    • if it is 1, the next three bits give "byte count", ie number of bytes in value.如果为 1,接下来的三位给出“字节数”,即值中的字节数。
      • mask out top nibble (flag bit plus byte count), concatenate the appropriate number of bytes and you've got the value.屏蔽顶部半字节(标志位加上字节数),连接适当数量的字节,你就得到了值。

I have no idea if this is a "real" format or my (former) coworker created this one himself (which is why I don't post code).我不知道这是一种“真实”的格式,还是我的(前)同事自己创建的(这就是我不发布代码的原因)。

You might be interested in the following functions:您可能对以下功能感兴趣:

htonl, htons, ntohl, ntohs - convert values between host and network byte order htonl, htons, ntohl, ntohs - 在主机和网络字节顺序之间转换值

   uint32_t htonl(uint32_t hostlong);
   uint16_t htons(uint16_t hostshort);
   uint32_t ntohl(uint32_t netlong);
   uint16_t ntohs(uint16_t netshort);

man byteorder

Text would be my first choice.文本将是我的第一选择。 If you want a varying length binary encoding you have two basic choices:如果你想要一个可变长度的二进制编码,你有两个基本的选择:

  • a length indication长度指示
  • an end marker结束标记

You obviously make merge those with some value bits.您显然将它们与一些值位合并。

  • For a length indication that would give you something where the length and some bits are given together (see for instance UTF-8),对于一个长度指示,它会给你一些长度和一些位一起给出的东西(参见例如 UTF-8),

  • For an end marker, you can for instance state that MSB set indicates the last byte and thus have 7 data bits per byte.对于结束标记,您可以例如 state 设置的 MSB 指示最后一个字节,因此每个字节有 7 个数据位。

Other variants are obviously possible.其他变体显然是可能的。

You could try Network Byte Order您可以尝试 网络字节顺序

Google's protocol buffers provide a pre-made implementation that uses variable-width encodings. Google 的协议缓冲区提供了使用可变宽度编码的预制实现。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM