QString str = "Hello, World!"; QByteArray byteArray = str.toUtf8(); // convert string to UTF-8 encoded byte array QByteArray percentEncoded = byteArray.toPercentEncoding(); // encode byte array as percent-encoded string qDebug() << percentEncoded; // output: "Hello%2C%20World%21"
QByteArray byteArray = "Hello, World!"; QByteArray percentEncoded = byteArray.toPercentEncoding(QByteArray(), QByteArray(), ','); // encode byte array as percent-encoded string, with commas not encoded qDebug() << percentEncoded; // output: "Hello,%20World!"In this example, we encode a QByteArray directly without first converting it from a string. We use the toPercentEncoding() function with some additional parameters to specify that commas should not be percent-encoded. These examples are part of the Qt library, which is a set of C++ libraries and development tools for building cross-platform applications. Specifically, the QByteArray class is part of the QtCore package, which includes the core non-graphical classes used by other Qt modules.