在谷歌游戏服务上保存游戏所需的数据格式是:std::vector<uint8_t>
,如“数据格式”下所述:
https://developers.google.com/games/services/cpp/savedgames
我认为这个向量表示一种字节数组。这正确吗?那么如何将std::string
转换为std::vector<uint8_t>
?
在谷歌游戏服务上保存游戏所需的数据格式是:std::vector<uint8_t>
,如“数据格式”下所述:
https://developers.google.com/games/services/cpp/savedgames
我认为这个向量表示一种字节数组。这正确吗?那么如何将std::string
转换为std::vector<uint8_t>
?
std::vector
有一个专门用于此目的的构造函数:
std::string str;
std::vector<uint8_t> vec(str.begin(), str.end());
std::vector<uint8_t>
不应被视为字符串。 - stefaanvstd::string
的存储空间末尾有一个额外的空字节,但它不是字符串本身的一部分。 - T.C.除了DeiDei的回答之外,如果向量已经构建好了,您可以执行以下操作:
std::string str;
std::vector<uint8_t> vec;
...
vec.assign(str.begin(), str.end());