带有Protobuf序列化的C++Hazelcast:字符串不是UTF-8格式的
C++ Hazelcast with Protobuf serialization: String is not UTF-8 formatted
我希望能够使用HazelCast发送序列化的Protobuf数据。我理解为什么它会给我错误,但有办法解决吗?
这是我正在使用的示例代码:
main.cpp
#include <iostream>
#include <vector>
#include <hazelcast/client/HazelcastClient.h>
#include "testProto.pb.h"
using namespace std;
int main(){
// create object
tutorial::Input protoInput;
protoInput.set_innum(500);
// buffer to store serialized string
string stringBuffer;
protoInputer.SerializeToString(&stringBuffer);
// set up hazelcast client
hazelcast::client::ClientConfig(config);
hazelcast::client::HazelcastClient hz(config);
hazelcast::client::IMap<string,string> map = hz.getMap<string,string>("myMap");
//error is from trying to write it
map.put("Input", stringBuffer);
return 0;
}
testProto.proto
syntax = "proto3";
package tutorial;
message Input{
int64 inNum = 1;
}
来自protobuf文档:
SerializeToString(string*输出(常量;
序列化消息并将字节存储在给定字符串中。请注意,字节是二进制的,而不是文本;我们只使用字符串类作为一个方便的容器。
因此,将它们作为字符串存储在IMap中是不安全的,因为在非UTF-8格式的情况下很容易失败。我建议您将protobuf存储为字节向量,其中hazelcast::byte
是unsigned char
:
hz.getMap<string, vector<hazelcast::byte>>("proto_map");
相关文章:
- HEX值到wchar_t字符(UTF-8)的转换
- 如何在openssl-ecc中获取十六进制格式的私钥
- 将"打开的CV图像"中的"颜色"转换为整数格式
- TDateTime格式在C++Builder中不会更改
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 检查不带转换的扫描格式
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- 是否可以从格式字符串中检索"width"
- clang格式:宏的缩进
- clang格式:禁用排序包含
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 将RGB图像保存为PPM格式
- 询问在设计我的手臂模拟器功能表示格式1
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 带有Protobuf序列化的C++Hazelcast:字符串不是UTF-8格式的
- 转换特殊字符(UTF-8)
- 从包含 utf-8(印地语)格式文本的文件读取并写入其他文件
- UTF 8 以 XML 格式编码的日语字符串
- 迭代utf-8格式的字符块
- 按字节顺序读取UTF-16格式的文件到wstring