#include#include int main() { QByteArray byteArray; // set the byte array by passing in a char array literal byteArray = "hello"; // append more values to the array byteArray.append(" world"); // print the contents of the array qDebug() << byteArray; // output: "hello world" return 0; }
#includeIn this example, we start with a QString object containing the text "hello". We then convert this to a QByteArray using the toUtf8() method. This method returns a QByteArray containing the UTF-8 representation of the underlying string. Package library: Qt.#include #include int main() { QString str = "hello"; // convert a QString to a QByteArray QByteArray byteArray = str.toUtf8(); // print the contents of the array qDebug() << byteArray; // output: "hello" return 0; }