24

The data format required to save games on google play game services is : std::vector<uint8_t> as specified under 'Data formats' on: https://developers.google.com/games/services/cpp/savedgames

I am assuming the vector represents some kind of byte array. Is that correct ? So how does one convert an std::string to std::vector<uint8_t> ?

Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190
  • 1
    If you want to do it char-by-char, you could loop and cast/add elements to a vector... – qxz Jan 19 '17 at 08:40

2 Answers2

44

std::vector has a constructor just for this purpose:

std::string str;
std::vector<uint8_t> vec(str.begin(), str.end());
DeiDei
  • 10,205
  • 6
  • 55
  • 80
10

Adding to DeiDei's answer, you can do the following if the vector is already constructed:

std::string str;
std::vector<uint8_t> vec;
...
vec.assign(str.begin(), str.end());
Jesse Jutson
  • 143
  • 1
  • 7