Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>QByteArray</code> has a <a href="http://doc.qt.nokia.com/latest/qbytearray.html#QByteArray-3" rel="nofollow">constructor</a> that does just this. </p> <p>This is how to use it:</p> <pre><code>const char* data; int size; // these variables should be correctly set to your array and size QByteArray ba(data, size); </code></pre> <p>I used a pointer for the data, but arrays decay to pointers when passed as parameters so it's the same. This constructor will make a deep copy of the data. That means it will only take ownership of the copy it makes, and the data you used might need to be deallocated if it was allocated dynamically.</p> <p>For arrays of other types than <code>char</code> you are essentially doing serialization:</p> <pre><code>int data[30]; for (int i = 0; i &lt; 30; ++i) { ba.append((const char*)(data + i), sizeof(int)); } </code></pre> <p>The above code will insert the bytes that make up your ints into a <code>QByteArray</code>. Be careful though, since at this point indexing the <code>QByteArray</code> won't return the ints but the bytes they are made of.</p> <p>To convert a <a href="http://doc.qt.nokia.com/latest/qstring.html" rel="nofollow">QString</a> (which is not what you asked but added a comment asking it) use one of the appropriate member functions of <code>QString</code>:</p> <pre><code>QByteArray ba = str.toLocal8bit(); QByteArray ba2 = str.toLatin1(); QByteArray ba3 = str.toUtf8(); QByteArray ba4 = str.toAscii(); </code></pre> <p>To decide which one to use, you must know the encoding of your string. To convert between encodings, use <a href="http://doc.qt.nokia.com/latest/qtextcodec.html#details" rel="nofollow">QTextCodec</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload