File: printhex.h

package info (click to toggle)
wfview 2.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,256 kB
  • sloc: cpp: 43,386; ansic: 3,196; sh: 32; xml: 29; makefile: 11
file content (60 lines) | stat: -rw-r--r-- 1,466 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef PRINTHEX_H
#define PRINTHEX_H
#include <QByteArray>
#include <QString>

#include "logcategories.h"


QStringList inline getHexArray(const QByteArray &pdata)
{
    QString head = "---- Begin hex dump -----:";
    QString sdata("DATA:  ");
    QString index("INDEX: ");

    for(int i=0; i < pdata.length(); i++)
    {
        sdata.append(QString("%1 ").arg((quint8)pdata[i], 2, 16, QChar('0')) );
        index.append(QString("%1 ").arg(i, 2, 10, QChar('0')));
    }

    QString tail = "----  End hex dump  -----";

    return {head, sdata, index, tail};
}

QString inline getHex(const QByteArray &pdata)
{
    QString head = "---- Begin hex dump -----:\n";
    QString sdata("DATA:  ");
    QString index("INDEX: ");

    for(int i=0; i < pdata.length(); i++)
    {
        sdata.append(QString("%1 ").arg((quint8)pdata[i], 2, 16, QChar('0')) );
        index.append(QString("%1 ").arg(i, 2, 10, QChar('0')));
    }

        sdata.append("\n");
        index.append("\n");

    QString tail = "----  End hex dump  -----\n";
    return head + sdata + index + tail;
}

void inline printHexNow(const QByteArray &pdata, const QLoggingCategory &cat)
{
    QString d = getHex(pdata);
    // These lines are needed to keep the formatting as expected in the log file
    if(d.endsWith("\n"))
    {
        d.chop(1);
    }
    QStringList s = d.split("\n");
    for(int i=0; i < s.length(); i++)
    {
        qDebug(cat) << s.at(i);
    }
}

#endif // PRINTHEX_H