1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 **   * Redistributions of source code must retain the above copyright
25 **     notice, this list of conditions and the following disclaimer.
26 **   * Redistributions in binary form must reproduce the above copyright
27 **     notice, this list of conditions and the following disclaimer in
28 **     the documentation and/or other materials provided with the
29 **     distribution.
30 **   * Neither the name of The Qt Company Ltd nor the names of its
31 **     contributors may be used to endorse or promote products derived
32 **     from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 
wrapInFunction()52 void wrapInFunction()
53 {
54 
55 //! [0]
56 QByteArray ba("Hello");
57 //! [0]
58 
59 
60 //! [1]
61 QByteArray ba;
62 ba.resize(5);
63 ba[0] = 0x3c;
64 ba[1] = 0xb8;
65 ba[2] = 0x64;
66 ba[3] = 0x18;
67 ba[4] = 0xca;
68 //! [1]
69 
70 
71 //! [2]
72 for (int i = 0; i < ba.size(); ++i) {
73     if (ba.at(i) >= 'a' && ba.at(i) <= 'f')
74         cout << "Found character in range [a-f]" << Qt::endl;
75 }
76 //! [2]
77 
78 
79 //! [3]
80 QByteArray x("and");
81 x.prepend("rock ");         // x == "rock and"
82 x.append(" roll");          // x == "rock and roll"
83 x.replace(5, 3, "&");       // x == "rock & roll"
84 //! [3]
85 
86 
87 //! [4]
88 QByteArray ba("We must be <b>bold</b>, very <b>bold</b>");
89 int j = 0;
90 while ((j = ba.indexOf("<b>", j)) != -1) {
91     cout << "Found <b> tag at index position " << j << Qt::endl;
92     ++j;
93 }
94 //! [4]
95 
96 
97 //! [5]
98 QByteArray().isNull();          // returns true
99 QByteArray().isEmpty();         // returns true
100 
101 QByteArray("").isNull();        // returns false
102 QByteArray("").isEmpty();       // returns true
103 
104 QByteArray("abc").isNull();     // returns false
105 QByteArray("abc").isEmpty();    // returns false
106 //! [5]
107 
108 
109 //! [6]
110 QByteArray ba("Hello");
111 int n = ba.size();          // n == 5
112 ba.data()[0];               // returns 'H'
113 ba.data()[4];               // returns 'o'
114 ba.data()[5];               // returns '\0'
115 //! [6]
116 
117 
118 //! [7]
119 QByteArray().isEmpty();         // returns true
120 QByteArray("").isEmpty();       // returns true
121 QByteArray("abc").isEmpty();    // returns false
122 //! [7]
123 
124 
125 //! [8]
126 QByteArray ba("Hello world");
127 char *data = ba.data();
128 while (*data) {
129     cout << "[" << *data << "]" << Qt::endl;
130     ++data;
131 }
132 //! [8]
133 
134 
135 //! [9]
136 QByteArray ba("Hello, world");
137 cout << ba[0]; // prints H
138 ba[7] = 'W';
139 // ba == "Hello, World"
140 //! [9]
141 
142 
143 //! [10]
144 QByteArray ba("Stockholm");
145 ba.truncate(5);             // ba == "Stock"
146 //! [10]
147 
148 
149 //! [11]
150 QByteArray ba("STARTTLS\r\n");
151 ba.chop(2);                 // ba == "STARTTLS"
152 //! [11]
153 
154 
155 //! [12]
156 QByteArray x("free");
157 QByteArray y("dom");
158 x += y;
159 // x == "freedom"
160 //! [12]
161 
162 
163 //! [13]
164 QByteArray().isNull();          // returns true
165 QByteArray("").isNull();        // returns false
166 QByteArray("abc").isNull();     // returns false
167 //! [13]
168 
169 
170 //! [14]
171 QByteArray ba("Istambul");
172 ba.fill('o');
173 // ba == "oooooooo"
174 
175 ba.fill('X', 2);
176 // ba == "XX"
177 //! [14]
178 
179 
180 //! [15]
181 QByteArray x("ship");
182 QByteArray y("air");
183 x.prepend(y);
184 // x == "airship"
185 //! [15]
186 
187 
188 //! [16]
189 QByteArray x("free");
190 QByteArray y("dom");
191 x.append(y);
192 // x == "freedom"
193 //! [16]
194 
195 
196 //! [17]
197 QByteArray ba("Meal");
198 ba.insert(1, QByteArray("ontr"));
199 // ba == "Montreal"
200 //! [17]
201 
202 
203 //! [18]
204 QByteArray ba("Montreal");
205 ba.remove(1, 4);
206 // ba == "Meal"
207 //! [18]
208 
209 
210 //! [19]
211 QByteArray x("Say yes!");
212 QByteArray y("no");
213 x.replace(4, 3, y);
214 // x == "Say no!"
215 //! [19]
216 
217 
218 //! [20]
219 QByteArray ba("colour behaviour flavour neighbour");
220 ba.replace(QByteArray("ou"), QByteArray("o"));
221 // ba == "color behavior flavor neighbor"
222 //! [20]
223 
224 
225 //! [21]
226 QByteArray x("sticky question");
227 QByteArray y("sti");
228 x.indexOf(y);               // returns 0
229 x.indexOf(y, 1);            // returns 10
230 x.indexOf(y, 10);           // returns 10
231 x.indexOf(y, 11);           // returns -1
232 //! [21]
233 
234 
235 //! [22]
236 QByteArray ba("ABCBA");
237 ba.indexOf("B");            // returns 1
238 ba.indexOf("B", 1);         // returns 1
239 ba.indexOf("B", 2);         // returns 3
240 ba.indexOf("X");            // returns -1
241 //! [22]
242 
243 
244 //! [23]
245 QByteArray x("crazy azimuths");
246 QByteArray y("az");
247 x.lastIndexOf(y);           // returns 6
248 x.lastIndexOf(y, 6);        // returns 6
249 x.lastIndexOf(y, 5);        // returns 2
250 x.lastIndexOf(y, 1);        // returns -1
251 //! [23]
252 
253 
254 //! [24]
255 QByteArray ba("ABCBA");
256 ba.lastIndexOf("B");        // returns 3
257 ba.lastIndexOf("B", 3);     // returns 3
258 ba.lastIndexOf("B", 2);     // returns 1
259 ba.lastIndexOf("X");        // returns -1
260 //! [24]
261 
262 
263 //! [25]
264 QByteArray url("ftp://ftp.qt-project.org/");
265 if (url.startsWith("ftp:"))
266     ...
267 //! [25]
268 
269 
270 //! [26]
271 QByteArray url("http://qt-project.org/doc/qt-5.0/qtdoc/index.html");
272 if (url.endsWith(".html"))
273     ...
274 //! [26]
275 
276 
277 //! [27]
278 QByteArray x("Pineapple");
279 QByteArray y = x.left(4);
280 // y == "Pine"
281 //! [27]
282 
283 
284 //! [28]
285 QByteArray x("Pineapple");
286 QByteArray y = x.right(5);
287 // y == "apple"
288 //! [28]
289 
290 
291 //! [29]
292 QByteArray x("Five pineapples");
293 QByteArray y = x.mid(5, 4);     // y == "pine"
294 QByteArray z = x.mid(5);        // z == "pineapples"
295 //! [29]
296 
297 
298 //! [30]
299 QByteArray x("Qt by THE QT COMPANY");
300 QByteArray y = x.toLower();
301 // y == "qt by the qt company"
302 //! [30]
303 
304 
305 //! [31]
306 QByteArray x("Qt by THE QT COMPANY");
307 QByteArray y = x.toUpper();
308 // y == "QT BY THE QT COMPANY"
309 //! [31]
310 
311 
312 //! [32]
313 QByteArray ba("  lots\t of\nwhitespace\r\n ");
314 ba = ba.simplified();
315 // ba == "lots of whitespace";
316 //! [32]
317 
318 
319 //! [33]
320 QByteArray ba("  lots\t of\nwhitespace\r\n ");
321 ba = ba.trimmed();
322 // ba == "lots\t of\nwhitespace";
323 //! [33]
324 
325 
326 //! [34]
327 QByteArray x("apple");
328 QByteArray y = x.leftJustified(8, '.');   // y == "apple..."
329 //! [34]
330 
331 
332 //! [35]
333 QByteArray x("apple");
334 QByteArray y = x.rightJustified(8, '.');    // y == "...apple"
335 //! [35]
336 
337 
338 //! [36]
339 QByteArray str("FF");
340 bool ok;
341 int hex = str.toInt(&ok, 16);     // hex == 255, ok == true
342 int dec = str.toInt(&ok, 10);     // dec == 0, ok == false
343 //! [36]
344 
345 
346 //! [37]
347 QByteArray str("FF");
348 bool ok;
349 long hex = str.toLong(&ok, 16);   // hex == 255, ok == true
350 long dec = str.toLong(&ok, 10);   // dec == 0, ok == false
351 //! [37]
352 
353 
354 //! [38]
355 QByteArray string("1234.56");
356 bool ok;
357 double a = string.toDouble(&ok);   // a == 1234.56, ok == true
358 
359 string = "1234.56 Volt";
360 a = str.toDouble(&ok);             // a == 0, ok == false
361 //! [38]
362 
363 //! [38float]
364 QByteArray string("1234.56");
365 bool ok;
366 float a = string.toFloat(&ok);    // a == 1234.56, ok == true
367 
368 string = "1234.56 Volt";
369 a = str.toFloat(&ok);              // a == 0, ok == false
370 //! [38float]
371 
372 //! [39]
373 QByteArray text("Qt is great!");
374 text.toBase64();        // returns "UXQgaXMgZ3JlYXQh"
375 //! [39]
376 
377 //! [39bis]
378 QByteArray text("<p>Hello?</p>");
379 text.toBase64(QByteArray::Base64Encoding | QByteArray::OmitTrailingEquals);      // returns "PHA+SGVsbG8/PC9wPg"
380 text.toBase64(QByteArray::Base64Encoding);                                       // returns "PHA+SGVsbG8/PC9wPg=="
381 text.toBase64(QByteArray::Base64UrlEncoding);                                    // returns "PHA-SGVsbG8_PC9wPg=="
382 text.toBase64(QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals);   // returns "PHA-SGVsbG8_PC9wPg"
383 //! [39bis]
384 
385 
386 //! [40]
387 QByteArray ba;
388 int n = 63;
389 ba.setNum(n);           // ba == "63"
390 ba.setNum(n, 16);       // ba == "3f"
391 //! [40]
392 
393 
394 //! [41]
395 int n = 63;
396 QByteArray::number(n);              // returns "63"
397 QByteArray::number(n, 16);          // returns "3f"
398 QByteArray::number(n, 16).toUpper();  // returns "3F"
399 //! [41]
400 
401 
402 //! [42]
403 QByteArray ba = QByteArray::number(12.3456, 'E', 3);
404 // ba == 1.235E+01
405 //! [42]
406 
407 
408 //! [43]
409  static const char mydata[] = {
410     '\x00', '\x00', '\x03', '\x84', '\x78', '\x9c', '\x3b', '\x76',
411     '\xec', '\x18', '\xc3', '\x31', '\x0a', '\xf1', '\xcc', '\x99',
412     ...
413     '\x6d', '\x5b'
414 };
415 
416 QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata));
417 QDataStream in(&data, QIODevice::ReadOnly);
418 ...
419 //! [43]
420 
421 
422 //! [44]
423 QByteArray text = QByteArray::fromBase64("UXQgaXMgZ3JlYXQh");
424 text.data();            // returns "Qt is great!"
425 //! [44]
426 
427 //! [44bis]
428 QByteArray::fromBase64("PHA+SGVsbG8/PC9wPg==", QByteArray::Base64Encoding); // returns "<p>Hello?</p>"
429 QByteArray::fromBase64("PHA-SGVsbG8_PC9wPg==", QByteArray::Base64UrlEncoding); // returns "<p>Hello?</p>"
430 //! [44bis]
431 
432 //! [44ter]
433 void process(const QByteArray &);
434 
435 if (auto result = QByteArray::fromBase64Encoding(encodedData))
436     process(*result);
437 //! [44ter]
438 
439 //! [44quater]
440 auto result = QByteArray::fromBase64Encoding(encodedData);
441 if (result.decodingStatus == QByteArray::Base64DecodingStatus::Ok)
442     process(result.decoded);
443 //! [44quater]
444 
445 
446 //! [45]
447 QByteArray text = QByteArray::fromHex("517420697320677265617421");
448 text.data();            // returns "Qt is great!"
449 //! [45]
450 
451 //! [46]
452 QString tmp = "test";
453 QByteArray text = tmp.toLocal8Bit();
454 char *data = new char[text.size()];
455 strcpy(data, text.data());
456 delete [] data;
457 //! [46]
458 
459 //! [47]
460 QString tmp = "test";
461 QByteArray text = tmp.toLocal8Bit();
462 char *data = new char[text.size() + 1];
463 strcpy(data, text.data());
464 delete [] data;
465 //! [47]
466 
467 //! [48]
468 QByteArray ba1("ca\0r\0t");
469 ba1.size();                     // Returns 2.
470 ba1.constData();                // Returns "ca" with terminating \0.
471 
472 QByteArray ba2("ca\0r\0t", 3);
473 ba2.size();                     // Returns 3.
474 ba2.constData();                // Returns "ca\0" with terminating \0.
475 
476 QByteArray ba3("ca\0r\0t", 4);
477 ba3.size();                     // Returns 4.
478 ba3.constData();                // Returns "ca\0r" with terminating \0.
479 
480 const char cart[] = {'c', 'a', '\0', 'r', '\0', 't'};
481 QByteArray ba4(QByteArray::fromRawData(cart, 6));
482 ba4.size();                     // Returns 6.
483 ba4.constData();                // Returns "ca\0r\0t" without terminating \0.
484 //! [48]
485 
486 //! [49]
487 QByteArray ba("ab");
488 ba.repeated(4);             // returns "abababab"
489 //! [49]
490 
491 //! [50]
492 QByteArray macAddress = QByteArray::fromHex("123456abcdef");
493 macAddress.toHex(':'); // returns "12:34:56:ab:cd:ef"
494 macAddress.toHex(0);   // returns "123456abcdef"
495 //! [50]
496 
497 //! [51]
498 QByteArray text = QByteArray::fromPercentEncoding("Qt%20is%20great%33");
499 text.data();            // returns "Qt is great!"
500 //! [51]
501 
502 //! [52]
503 QByteArray text = "{a fishy string?}";
504 QByteArray ba = text.toPercentEncoding("{}", "s");
505 qDebug(ba.constData());
506 // prints "{a fi%73hy %73tring%3F}"
507 //! [52]
508 
509 //! [53]
510 QByteArray ba = QByteArrayLiteral("byte array contents");
511 //! [53]
512 
513 }
514 
515 
516