title: Good methods for converting char array buffers to strings?
body: <p>I am relatively new to C++.  Recent assignments have required that I convert a multitude of char buffers (from structures/sockets, etc.) to strings.  I have been using variations on the following but they seem awkward.  Is there a better way to do this kind of thing?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  using std::string;
  using std::cout;
  using std::endl;
  
  
  char* bufferToCString(char *buff, int buffSize, char *str)
  {
      memset(str, '\0', buffSize + 1);
      return(strncpy(str, buff, buffSize));
  }
  
  
  string&amp; bufferToString(char* buffer, int bufflen, string&amp; str)
  {
      char temp[bufflen];
  
      memset(temp, '\0', bufflen + 1);
      strncpy(temp, buffer, bufflen);
  
      return(str.assign(temp));
  }
  
  
  
  int main(int argc, char *argv[])
  {
     char buff[4] = {'a', 'b', 'c', 'd'};
  
     char str[5];
     string str2;
  
     cout &lt;&lt; bufferToCString(buff, sizeof(buff), str) &lt;&lt; endl;
  
     cout &lt;&lt; bufferToString(buff, sizeof(buff), str2) &lt;&lt; endl;
  
  }
  </code></pre>