HexTool/data.cpp
2018-05-30 22:44:36 +08:00

89 lines
1.6 KiB
C++

#include "data.h"
#include <QTime>
#include <QtGlobal>
Data::Data(QString &in, bool cap)
{
this->txt = in;
this->cap = cap;
}
Data::Data(int len, bool cap)
{
QByteArray rand;
QTime cur = QTime::currentTime();
qsrand(cur.second() * 1000 + cur.msec());
while (len--) rand.append(qrand() % 256);
this->txt = QString(rand.toHex());
this->cap = cap;
}
void Data::to_hex()
{
this->txt = QString(this->txt.toLocal8Bit().toHex());
}
void Data::clean()
{
this->txt = this->txt.toLower().replace(QRegExp("(0x)|[^0-9a-fA-F\n]"), "");
}
QString Data::get_ascii()
{
return QString(QByteArray::fromHex(this->txt.toLocal8Bit()));
}
QString Data::get(int unit, enum format fmt)
{
QStringList work;
if (this->cap) this->txt = this->txt.toUpper();
if (0 > unit) {
work = this->txt.split('\n');
} else {
this->txt.replace("\n", "");
if (unit) {
unit *= 2;
for (int i = 0; i < this->txt.size(); i += unit) {
work << this->txt.mid(i, unit);
}
} else {
work << this->txt;
}
}
QString st;
switch (fmt) {
case Data::seq:
return work.join('\n');
case Data::space:
st = "\\1 ";
break;
case Data::prefix:
st = "0x\\1, ";
break;
default:
return "";
}
QStringList::iterator pick;
for (pick = work.begin(); pick != work.end(); pick++) {
if ((*pick).size() & 1) (*pick) += "0";
(*pick).replace(QRegExp("([0-9a-fA-F]{2})"), st);
(*pick).replace(QRegExp(" +$"), "");
}
return work.join('\n');
}