title: how to convert std::string to NSData and vice versa?
body: <p>I am using google protocol buffer to send and receive data in cocos2d-x multiplayer game via Google Play Games Services iOS sdk.</p>
  
  <p>Protocol buffer converts data to std::string but GPGS iOS sdk sends data via NSData hence I have to convert from std::string to NSData and then back to std::string after receiving data.</p>
  
  <p>I am currently using following method:</p>
  
  <p>(<strong>std::string to NSData</strong> and <strong>NSData to std::string</strong> will be done in different functions at different times. Following code just summarise what I am doing overall)</p>
  
  <pre><code>//PlayerData is protocol buffer class
  PlayerData data, temp;
  
  std::string dataStr;
  data.SerializeToString(&amp;dataStr);
  
  NSString* nsDataStr = [NSString stringWithCString:dataStr.c_str()
                          encoding:[NSString defaultCStringEncoding]];
  
  NSData* nsData = [nsDataStr dataUsingEncoding:NSUTF8StringEncoding];
  
  NSString* dataStr_2 = [[NSString alloc] initWithData:nsData
                          encoding:NSUTF8StringEncoding];
  
  std::string foo = [dataStr_2 UTF8String];
  
  temp.ParseFromString(foo);
  </code></pre>
  
  <p><strong>Initial string i.e dataStr after serializing</strong></p>
  
  <p>"\r\x95n\x99D\x158\xddNDJ\nUmar SaeedR\x12p_CPH64oqq2K-TXxAB"</p>
  
  <p><strong>size: 42</strong></p>
  
  <p><strong>Final string i.e foo before parsing</strong></p>
  
  <p>"\r\xc3\xafn\xc3\xb4D\x158\xe2\x80\xbaNDJ\nUmar SaeedR\x12p_CPH64oqq2K-TXxAB"</p>
  
  <p><strong>size: 46</strong></p>
  
  <p>the ParseFromString function of protocol buffer does not parse foo and returns false.</p>
  
  <p><strong>How to do string conversions so that string remains same?</strong></p>