title: how to correctly converting char array to string in c++
body: <p>I have these two set of char arrays:</p>
  
  <pre><code>  char t1[]={'1','2','\0','\0'};
    char t2[]={'1','2','3','4'};
  </code></pre>
  
  <p>I want to write a function to convert them to string, but the string size for t1 should be 2 and for t2 should be 4.</p>
  
  <pre><code> string convert(char * data)
   {
        return string(data);
   }
  </code></pre>
  
  <p>Does a good job for t1, but crashes on t2 (t2 is not null terminated).</p>
  
  <pre><code> string convert(char * data)
   {
        return string(data,data+4);
   }
  </code></pre>
  
  <p>Does a good job for t2, but the size of generate string for t1 is 4 and not 2.</p>
  
  <p>What is the best way to write a simple and fast function to do this correctly?</p>