如何将标准::字符串转换为标准::矢量<uint8_t>?
How to convert std::string to std::vector<uint8_t>?
在
Google Play 游戏服务上保存游戏所需的数据格式为: 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());
除了DeiDei的答案之外,如果向量已经构造好,你可以执行以下操作:
std::string str;
std::vector<uint8_t> vec;
...
vec.assign(str.begin(), str.end());
相关文章:
- 使用CMake检测支持的C++标准
- 如何理解C++标准N3337中的expr.const.cast子句8
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 编译标准库类型
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 编译器如何在使用SFINAE的函数和标准函数之间确定两者是否可行
- 从值小于256的uint16到uint8的Endian安全转换
- 铸造标准::有没有回到原来的类型
- 标准 N3337 5.2.10 第 7 条中的C++"类型"是什么意思?
- this_thread::sleep_for和计时时钟之间的关系是否由C++11标准指定
- 标准库类型的赋值运算符的引用限定符
- 标准是否严格定义了该程序应该如何编译?
- 如何从Windows应用程序输出到标准?
- 安全到标准:移动会员?
- 如何正确将字符串转换为标准::时间::system_clock::time_point?
- 这是否符合C++标准:双响双响,例如!!(-0.0).
- 标准::变体的赋值运算符
- 捕获标准输出以压缩并使用 CTRL-C 中断会给出损坏的 zip 文件
- uint32, int16, uint8 .为什么这些常用的数据类型没有进入标准
- 兼容的声明 __attribute__ ((节( ".abc.dfe" ))) 常量易失性 uint8 属性变量 = 0;- 符合MISRA标准