c++ - 如何在C++中对protobuf的二进制字符串进行十六进制化?

标签 c++ protocol-buffers

例如,我的protobuf消息是

message Person {
  required string name=1;
}

如果我使用Python:

import personinfo_pb2
person = personinfo_pb2.Person()
person.name="Houl"

packed_data = person.SerializeToString()

我可以通过以下方式打印 hexlify 数据:

print binascii.hexlify(packed_data)

我想知道如何在 C++ 中对 SerializeToString 数据进行十六进制化?

最佳答案

您需要自己实现十六进制编码。例如,一种可能的实现可能是:

string HexEscape(const string& buf) {
  string result(buf.size() * 3 + 1, '\0');
  const char hex[] = "0123456789ABCDEF";

  for(size_t i = 0; i < buf.size(); ++i) {
    char* dest = &result[i*3];
    *dest++ = hex[(buf[i]>>4) & 0xF];
    *dest++ = hex[buf[i] & 0xF];
    *dest = ' ';
  }
  return result;
}

关于c++ - 如何在C++中对protobuf的二进制字符串进行十六进制化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33559032/

相关文章:

c++ - 是否建议使用 extern 来避免 header 依赖?

c++ - 特征提取 : Divide the ROI into small windows(patches) or let it be as ROI?

c++ - 是否存在合理的情况,其中 == 运算符不是 != 的否定?

go - 如何在protobuf中将自动生成的唯一ID字段设置为只读?

c++ - 解析位置参数

c++ - for 循环不能正常工作

windows - 构建和使用 Google Protocol Buffers

java - 在输出文件中找不到 protobuf 服务

c++ - Protocol Buffer : Maximum memory size for the Arena

python - Tensorflow:创建 tf.NodeDef() 并设置属性