Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
src_corelib_text_qbytearray.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4
6{
7
9QByteArray ba("Hello");
11
12
15ba.resize(5);
16ba[0] = 0x3c;
17ba[1] = 0xb8;
18ba[2] = 0x64;
19ba[3] = 0x18;
20ba[4] = 0xca;
22
23
25for (qsizetype i = 0; i < ba.size(); ++i) {
26 if (ba.at(i) >= 'a' && ba.at(i) <= 'f')
27 cout << "Found character in range [a-f]" << endl;
28}
30
31
33QByteArray x("and");
34x.prepend("rock "); // x == "rock and"
35x.append(" roll"); // x == "rock and roll"
36x.replace(5, 3, "&"); // x == "rock & roll"
38
39
41QByteArray ba("We must be <b>bold</b>, very <b>bold</b>");
42qsizetype j = 0;
43while ((j = ba.indexOf("<b>", j)) != -1) {
44 cout << "Found <b> tag at index position " << j << endl;
45 ++j;
46}
48
49
51QByteArray().isNull(); // returns true
52QByteArray().isEmpty(); // returns true
53
54QByteArray("").isNull(); // returns false
55QByteArray("").isEmpty(); // returns true
56
57QByteArray("abc").isNull(); // returns false
58QByteArray("abc").isEmpty(); // returns false
60
61
63QByteArray ba("Hello");
64qsizetype n = ba.size(); // n == 5
65ba.data()[0]; // returns 'H'
66ba.data()[4]; // returns 'o'
67ba.data()[5]; // returns '\0'
69
70
72QByteArray().isEmpty(); // returns true
73QByteArray("").isEmpty(); // returns true
74QByteArray("abc").isEmpty(); // returns false
76
77
79QByteArray ba("Hello world");
80char *data = ba.data();
81while (*data) {
82 cout << "[" << *data << "]" << endl;
83 ++data;
84}
86
87
89QByteArray ba("Hello, world");
90cout << ba[0]; // prints H
91ba[7] = 'W';
92// ba == "Hello, World"
94
95
97QByteArray ba("Stockholm");
98ba.truncate(5); // ba == "Stock"
100
101
103QByteArray ba("STARTTLS\r\n");
104ba.chop(2); // ba == "STARTTLS"
106
107
109QByteArray x("free");
110QByteArray y("dom");
111x += y;
112// x == "freedom"
114
115
117QByteArray().isNull(); // returns true
118QByteArray("").isNull(); // returns false
119QByteArray("abc").isNull(); // returns false
121
122
124QByteArray ba("Istambul");
125ba.fill('o');
126// ba == "oooooooo"
127
128ba.fill('X', 2);
129// ba == "XX"
131
132
134QByteArray x("ship");
135QByteArray y("air");
136x.prepend(y);
137// x == "airship"
139
140
142QByteArray x("free");
143QByteArray y("dom");
144x.append(y);
145// x == "freedom"
147
148
150QByteArray ba("Meal");
151ba.insert(1, QByteArrayView("ontr"));
152// ba == "Montreal"
154
155
157QByteArray ba("Montreal");
158ba.remove(1, 4);
159// ba == "Meal"
161
162
164QByteArray x("Say yes!");
165QByteArray y("no");
166x.replace(4, 3, y);
167// x == "Say no!"
169
170
172QByteArray ba("colour behaviour flavour neighbour");
173ba.replace(QByteArray("ou"), QByteArray("o"));
174// ba == "color behavior flavor neighbor"
176
177
179QByteArray x("sticky question");
180QByteArrayView y("sti");
181x.indexOf(y); // returns 0
182x.indexOf(y, 1); // returns 10
183x.indexOf(y, 10); // returns 10
184x.indexOf(y, 11); // returns -1
186
187
189QByteArray ba("ABCBA");
190ba.indexOf("B"); // returns 1
191ba.indexOf("B", 1); // returns 1
192ba.indexOf("B", 2); // returns 3
193ba.indexOf("X"); // returns -1
195
196
198QByteArray x("crazy azimuths");
199QByteArrayView y("az");
200x.lastIndexOf(y); // returns 6
201x.lastIndexOf(y, 6); // returns 6
202x.lastIndexOf(y, 5); // returns 2
203x.lastIndexOf(y, 1); // returns -1
205
206
208QByteArray ba("ABCBA");
209ba.lastIndexOf("B"); // returns 3
210ba.lastIndexOf("B", 3); // returns 3
211ba.lastIndexOf("B", 2); // returns 1
212ba.lastIndexOf("X"); // returns -1
214
215
217QByteArray url("ftp://ftp.qt-project.org/");
218if (url.startsWith("ftp:"))
219 ...
221
222
224QByteArray url("http://qt-project.org/doc/qt-5.0/qtdoc/index.html");
225if (url.endsWith(".html"))
226 ...
228
229
231QByteArray x("Pineapple");
232QByteArray y = x.first(4);
233// y == "Pine"
235
236
238QByteArray x("Pineapple");
239QByteArray y = x.last(5);
240// y == "apple"
242
243
245QByteArray x("Five pineapples");
246QByteArray y = x.sliced(5, 4); // y == "pine"
247QByteArray z = x.sliced(5); // z == "pineapples"
249
250
252QByteArray x("Qt by THE QT COMPANY");
254// y == "qt by the qt company"
256
257
259QByteArray x("Qt by THE QT COMPANY");
261// y == "QT BY THE QT COMPANY"
263
264
266QByteArray ba(" lots\t of\nwhitespace\r\n ");
267ba = ba.simplified();
268// ba == "lots of whitespace";
270
271
273QByteArray ba(" lots\t of\nwhitespace\r\n ");
274ba = ba.trimmed();
275// ba == "lots\t of\nwhitespace";
277
278
280QByteArray x("apple");
281QByteArray y = x.leftJustified(8, '.'); // y == "apple..."
283
284
286QByteArray x("apple");
287QByteArray y = x.rightJustified(8, '.'); // y == "...apple"
289
290
292QByteArray str("FF");
293bool ok;
294int hex = str.toInt(&ok, 16); // hex == 255, ok == true
295int dec = str.toInt(&ok, 10); // dec == 0, ok == false
297
298
300QByteArray str("FF");
301bool ok;
302long hex = str.toLong(&ok, 16); // hex == 255, ok == true
303long dec = str.toLong(&ok, 10); // dec == 0, ok == false
305
306
308QByteArray string("1234.56");
309bool ok;
310double a = string.toDouble(&ok); // a == 1234.56, ok == true
311
312string = "1234.56 Volt";
313a = str.toDouble(&ok); // a == 0, ok == false
315
317QByteArray string("1234.56");
318bool ok;
319float a = string.toFloat(&ok); // a == 1234.56, ok == true
320
321string = "1234.56 Volt";
322a = str.toFloat(&ok); // a == 0, ok == false
324
326QByteArray text("Qt is great!");
327text.toBase64(); // returns "UXQgaXMgZ3JlYXQh"
328
329QByteArray text("<p>Hello?</p>");
330text.toBase64(QByteArray::Base64Encoding | QByteArray::OmitTrailingEquals); // returns "PHA+SGVsbG8/PC9wPg"
331text.toBase64(QByteArray::Base64Encoding); // returns "PHA+SGVsbG8/PC9wPg=="
332text.toBase64(QByteArray::Base64UrlEncoding); // returns "PHA-SGVsbG8_PC9wPg=="
333text.toBase64(QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals); // returns "PHA-SGVsbG8_PC9wPg"
335
338int n = 63;
339ba.setNum(n); // ba == "63"
340ba.setNum(n, 16); // ba == "3f"
342
343
345int n = 63;
346QByteArray::number(n); // returns "63"
347QByteArray::number(n, 16); // returns "3f"
348QByteArray::number(n, 16).toUpper(); // returns "3F"
350
351
353QByteArray ba = QByteArray::number(12.3456, 'E', 3);
354// ba == 1.235E+01
356
357
359 static const char mydata[] = {
360 '\x00', '\x00', '\x03', '\x84', '\x78', '\x9c', '\x3b', '\x76',
361 '\xec', '\x18', '\xc3', '\x31', '\x0a', '\xf1', '\xcc', '\x99',
362 ...
363 '\x6d', '\x5b'
364};
365
366QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata));
368...
370
371
373QByteArray text = QByteArray::fromBase64("UXQgaXMgZ3JlYXQh");
374text.data(); // returns "Qt is great!"
375
376QByteArray::fromBase64("PHA+SGVsbG8/PC9wPg==", QByteArray::Base64Encoding); // returns "<p>Hello?</p>"
377QByteArray::fromBase64("PHA-SGVsbG8_PC9wPg==", QByteArray::Base64UrlEncoding); // returns "<p>Hello?</p>"
379
381void process(const QByteArray &);
382
383if (auto result = QByteArray::fromBase64Encoding(encodedData))
384 process(*result);
386
388auto result = QByteArray::fromBase64Encoding(encodedData);
389if (result.decodingStatus == QByteArray::Base64DecodingStatus::Ok)
390 process(result.decoded);
392
394QByteArray text = QByteArray::fromHex("517420697320677265617421");
395text.data(); // returns "Qt is great!"
397
399QString tmp = "test";
401char *data = new char[text.size()];
402strcpy(data, text.data());
403delete [] data;
405
407QString tmp = "test";
409char *data = new char[text.size() + 1];
410strcpy(data, text.data());
411delete [] data;
413
415QByteArray ba1("ca\0r\0t");
416ba1.size(); // Returns 2.
417ba1.constData(); // Returns "ca" with terminating \0.
418
419QByteArray ba2("ca\0r\0t", 3);
420ba2.size(); // Returns 3.
421ba2.constData(); // Returns "ca\0" with terminating \0.
422
423QByteArray ba3("ca\0r\0t", 4);
424ba3.size(); // Returns 4.
425ba3.constData(); // Returns "ca\0r" with terminating \0.
426
427const char cart[] = {'c', 'a', '\0', 'r', '\0', 't'};
429ba4.size(); // Returns 6.
430ba4.constData(); // Returns "ca\0r\0t" without terminating \0.
432
434QByteArray ba("ab");
435ba.repeated(4); // returns "abababab"
437
439QByteArray macAddress = QByteArray::fromHex("123456abcdef");
440macAddress.toHex(':'); // returns "12:34:56:ab:cd:ef"
441macAddress.toHex(0); // returns "123456abcdef"
443
445QByteArray text = QByteArray::fromPercentEncoding("Qt%20is%20great%33");
446qDebug("%s", text.data()); // reports "Qt is great!"
448
450QByteArray text = "{a fishy string?}";
451QByteArray ba = text.toPercentEncoding("{}", "s");
452qDebug("%s", ba.constData());
453// prints "{a fi%73hy %73tring%3F}"
455
457QByteArray ba = QByteArrayLiteral("byte array contents");
459
461QByteArray encoded("Qt%20is%20great%33");
462QByteArray decoded = encoded.percentDecoded(); // Set to "Qt is great!"
464
466emscripten::val uint8array = emscripten::val::global("g_uint8array");
467QByteArray byteArray = QByteArray::fromEcmaUint8Array(uint8array);
469
471QByteArray byteArray = "test";
472emscripten::val uint8array = QByteArray::toEcmaUint8Array(byteArray);
474
475}
\inmodule QtCore
Definition qbytearray.h:57
QByteArray trimmed() const &
Definition qbytearray.h:198
char * data()
\macro QT_NO_CAST_FROM_BYTEARRAY
Definition qbytearray.h:534
QByteArray repeated(qsizetype times) const
QByteArray & fill(char c, qsizetype size=-1)
Sets every byte in the byte array to ch.
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
Definition qbytearray.h:474
static QByteArray fromHex(const QByteArray &hexEncoded)
Returns a decoded copy of the hex encoded array hexEncoded.
QByteArray last(qsizetype n) const
Definition qbytearray.h:161
QByteArray simplified() const &
Definition qbytearray.h:202
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:122
QByteArray toUpper() const &
Definition qbytearray.h:194
QByteArray & setNum(short, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qbytearray.h:611
static QByteArray fromPercentEncoding(const QByteArray &pctEncoded, char percent='%')
qsizetype indexOf(char c, qsizetype from=0) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void chop(qsizetype n)
Removes n bytes from the end of the byte array.
void truncate(qsizetype pos)
Truncates the byte array at index position pos.
QByteArray percentDecoded(char percent='%') const
static QByteArray fromBase64(const QByteArray &base64, Base64Options options=Base64Encoding)
@ Base64UrlEncoding
Definition qbytearray.h:74
@ OmitTrailingEquals
Definition qbytearray.h:77
char at(qsizetype i) const
Returns the byte at index position i in the byte array.
Definition qbytearray.h:523
QByteArray & insert(qsizetype i, QByteArrayView data)
bool isEmpty() const noexcept
Returns true if the byte array has size 0; otherwise returns false.
Definition qbytearray.h:106
static QByteArray number(int, int base=10)
Returns a byte-array representing the whole number n as text.
QByteArray sliced(qsizetype pos) const
Definition qbytearray.h:163
QByteArray first(qsizetype n) const
Definition qbytearray.h:159
QByteArray leftJustified(qsizetype width, char fill=' ', bool truncate=false) const
Returns a byte array of size width that contains this byte array padded with the fill byte.
QByteArray toLower() const &
Definition qbytearray.h:190
QByteArray rightJustified(qsizetype width, char fill=' ', bool truncate=false) const
Returns a byte array of size width that contains the fill byte followed by this byte array.
qsizetype lastIndexOf(char c, qsizetype from=-1) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void resize(qsizetype size)
Sets the size of the byte array to size bytes.
QByteArray & remove(qsizetype index, qsizetype len)
Removes len bytes from the array, starting at index position pos, and returns a reference to the arra...
QByteArray toHex(char separator='\0') const
Returns a hex encoded copy of the byte array.
bool isNull() const noexcept
Returns true if this byte array is null; otherwise returns false.
static FromBase64Result fromBase64Encoding(QByteArray &&base64, Base64Options options=Base64Encoding)
static QByteArray fromRawData(const char *data, qsizetype size)
Constructs a QByteArray that uses the first size bytes of the data array.
Definition qbytearray.h:394
QByteArray & replace(qsizetype index, qsizetype len, const char *s, qsizetype alen)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qbytearray.h:275
\inmodule QtCore\reentrant
Definition qdatastream.h:30
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
double toDouble(bool *ok=nullptr) const
Returns the string converted to a double value.
Definition qstring.cpp:7642
int toInt(bool *ok=nullptr, int base=10) const
Returns the string converted to an int using base base, which is 10 by default and must be between 2 ...
Definition qstring.h:660
qsizetype size() const
Returns the number of characters in this string.
Definition qstring.h:182
long toLong(bool *ok=nullptr, int base=10) const
Returns the string converted to a long using base base, which is 10 by default and must be between 2 ...
Definition qstring.h:664
float toFloat(bool *ok=nullptr) const
Returns the string converted to a float value.
Definition qstring.cpp:7688
QByteArray toLocal8Bit() const &
Definition qstring.h:567
QChar * data()
Returns a pointer to the data stored in the QString.
Definition qstring.h:1095
QString str
[2]
QString text
#define QByteArrayLiteral(str)
Definition qbytearray.h:52
#define qDebug
[1]
Definition qlogging.h:160
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat z
GLint GLint GLint GLint GLint x
[0]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLfloat n
GLint y
GLuint in
GLuint64EXT * result
[6]
GLsizei const GLchar *const * string
[0]
Definition qopenglext.h:694
ptrdiff_t qsizetype
Definition qtypes.h:70
QByteArray ba
[0]
QUrl url("example.com")
[constructor-url-reference]
void wrapInFunction()