QByteArray: Overload toHex() with separator character
The separator character is inserted in the resulting array
after every byte and is useful for MAC address output like
01:23:45🆎cd:ef, Hash fingerprints, or low level data
debug output.
[ChangeLog][QtCore][QByteArray] Added toHex() overload to
insert a separator character between the hex bytes.
Change-Id: Ibe436094badc02f3ade7751aa8b5d690599941d4
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
8266de089c
commit
615027129d
@ -4355,12 +4355,41 @@ QByteArray QByteArray::fromHex(const QByteArray &hexEncoded)
|
||||
*/
|
||||
QByteArray QByteArray::toHex() const
|
||||
{
|
||||
QByteArray hex(d->size * 2, Qt::Uninitialized);
|
||||
return toHex('\0');
|
||||
}
|
||||
|
||||
/*! \overload
|
||||
\since 5.9
|
||||
|
||||
Returns a hex encoded copy of the byte array. The hex encoding uses the numbers 0-9 and
|
||||
the letters a-f.
|
||||
|
||||
If \a separator is not '\0', the separator character is inserted between the hex bytes.
|
||||
|
||||
Example:
|
||||
\code
|
||||
QByteArray macAddress = QByteArray::fromHex("123456abcdef");
|
||||
macAddress.toHex(':'); // returns "12:34:56:ab:cd:ef"
|
||||
macAddress.toHex(0); // returns "123456abcdef"
|
||||
\endcode
|
||||
|
||||
\sa fromHex()
|
||||
*/
|
||||
QByteArray QByteArray::toHex(char separator) const
|
||||
{
|
||||
if (!d->size)
|
||||
return QByteArray();
|
||||
|
||||
const int length = separator ? (d->size * 3 - 1) : (d->size * 2);
|
||||
QByteArray hex(length, Qt::Uninitialized);
|
||||
char *hexData = hex.data();
|
||||
const uchar *data = (const uchar *)d->data();
|
||||
for (int i = 0; i < d->size; ++i) {
|
||||
hexData[i*2] = QtMiscUtils::toHexLower(data[i] >> 4);
|
||||
hexData[i*2+1] = QtMiscUtils::toHexLower(data[i] & 0xf);
|
||||
for (int i = 0, o = 0; i < d->size; ++i) {
|
||||
hexData[o++] = QtMiscUtils::toHexLower(data[i] >> 4);
|
||||
hexData[o++] = QtMiscUtils::toHexLower(data[i] & 0xf);
|
||||
|
||||
if ((separator) && (o < length))
|
||||
hexData[o++] = separator;
|
||||
}
|
||||
return hex;
|
||||
}
|
||||
|
@ -357,6 +357,7 @@ public:
|
||||
QByteArray toBase64(Base64Options options) const;
|
||||
QByteArray toBase64() const; // ### Qt6 merge with previous
|
||||
QByteArray toHex() const;
|
||||
QByteArray toHex(char separator) const; // ### Qt6 merge with previous
|
||||
QByteArray toPercentEncoding(const QByteArray &exclude = QByteArray(),
|
||||
const QByteArray &include = QByteArray(),
|
||||
char percent = '%') const;
|
||||
|
@ -1443,61 +1443,97 @@ void tst_QByteArray::appendAfterFromRawData()
|
||||
void tst_QByteArray::toFromHex_data()
|
||||
{
|
||||
QTest::addColumn<QByteArray>("str");
|
||||
QTest::addColumn<char>("sep");
|
||||
QTest::addColumn<QByteArray>("hex");
|
||||
QTest::addColumn<QByteArray>("hex_alt1");
|
||||
|
||||
QTest::newRow("Qt is great!")
|
||||
QTest::newRow("Qt is great! (default)")
|
||||
<< QByteArray("Qt is great!")
|
||||
<< '\0'
|
||||
<< QByteArray("517420697320677265617421")
|
||||
<< QByteArray("51 74 20 69 73 20 67 72 65 61 74 21");
|
||||
|
||||
QTest::newRow("Qt is great! (with space)")
|
||||
<< QByteArray("Qt is great!")
|
||||
<< ' '
|
||||
<< QByteArray("51 74 20 69 73 20 67 72 65 61 74 21")
|
||||
<< QByteArray("51 74 20 69 73 20 67 72 65 61 74 21");
|
||||
|
||||
QTest::newRow("Qt is great! (with minus)")
|
||||
<< QByteArray("Qt is great!")
|
||||
<< '-'
|
||||
<< QByteArray("51-74-20-69-73-20-67-72-65-61-74-21")
|
||||
<< QByteArray("51-74-20-69-73-20-67-72-65-61-74-21");
|
||||
|
||||
QTest::newRow("Qt is so great!")
|
||||
<< QByteArray("Qt is so great!")
|
||||
<< '\0'
|
||||
<< QByteArray("517420697320736f20677265617421")
|
||||
<< QByteArray("51 74 20 69 73 20 73 6f 20 67 72 65 61 74 21");
|
||||
|
||||
QTest::newRow("default-constructed")
|
||||
<< QByteArray()
|
||||
<< '\0'
|
||||
<< QByteArray()
|
||||
<< QByteArray();
|
||||
|
||||
QTest::newRow("default-constructed (with space)")
|
||||
<< QByteArray()
|
||||
<< ' '
|
||||
<< QByteArray()
|
||||
<< QByteArray();
|
||||
|
||||
QTest::newRow("empty")
|
||||
<< QByteArray("")
|
||||
<< '\0'
|
||||
<< QByteArray("")
|
||||
<< QByteArray("");
|
||||
|
||||
QTest::newRow("empty (with space)")
|
||||
<< QByteArray("")
|
||||
<< ' '
|
||||
<< QByteArray("")
|
||||
<< QByteArray("");
|
||||
|
||||
QTest::newRow("array-of-null")
|
||||
<< QByteArray("\0", 1)
|
||||
<< '\0'
|
||||
<< QByteArray("00")
|
||||
<< QByteArray("0");
|
||||
|
||||
QTest::newRow("no-leading-zero")
|
||||
<< QByteArray("\xf")
|
||||
<< '\0'
|
||||
<< QByteArray("0f")
|
||||
<< QByteArray("f");
|
||||
|
||||
QTest::newRow("single-byte")
|
||||
<< QByteArray("\xaf")
|
||||
<< '\0'
|
||||
<< QByteArray("af")
|
||||
<< QByteArray("xaf");
|
||||
|
||||
QTest::newRow("no-leading-zero")
|
||||
<< QByteArray("\xd\xde\xad\xc0\xde")
|
||||
<< '\0'
|
||||
<< QByteArray("0ddeadc0de")
|
||||
<< QByteArray("ddeadc0de");
|
||||
|
||||
QTest::newRow("garbage")
|
||||
<< QByteArray("\xC\xde\xeC\xea\xee\xDe\xee\xee")
|
||||
<< '\0'
|
||||
<< QByteArray("0cdeeceaeedeeeee")
|
||||
<< QByteArray("Code less. Create more. Deploy everywhere.");
|
||||
|
||||
QTest::newRow("under-defined-1")
|
||||
<< QByteArray("\x1\x23")
|
||||
<< '\0'
|
||||
<< QByteArray("0123")
|
||||
<< QByteArray("x123");
|
||||
|
||||
QTest::newRow("under-defined-2")
|
||||
<< QByteArray("\x12\x34")
|
||||
<< '\0'
|
||||
<< QByteArray("1234")
|
||||
<< QByteArray("x1234");
|
||||
}
|
||||
@ -1505,15 +1541,22 @@ void tst_QByteArray::toFromHex_data()
|
||||
void tst_QByteArray::toFromHex()
|
||||
{
|
||||
QFETCH(QByteArray, str);
|
||||
QFETCH(char, sep);
|
||||
QFETCH(QByteArray, hex);
|
||||
QFETCH(QByteArray, hex_alt1);
|
||||
|
||||
{
|
||||
if (sep == 0) {
|
||||
const QByteArray th = str.toHex();
|
||||
QCOMPARE(th.size(), hex.size());
|
||||
QCOMPARE(th, hex);
|
||||
}
|
||||
|
||||
{
|
||||
const QByteArray th = str.toHex(sep);
|
||||
QCOMPARE(th.size(), hex.size());
|
||||
QCOMPARE(th, hex);
|
||||
}
|
||||
|
||||
{
|
||||
const QByteArray fh = QByteArray::fromHex(hex);
|
||||
QCOMPARE(fh.size(), str.size());
|
||||
|
Loading…
Reference in New Issue
Block a user