update
This commit is contained in:
parent
6f68eaac30
commit
5599424b71
|
@ -204,3 +204,47 @@ QVariant BytesAsInteger::parse(const QByteArray& bytes) const
|
|||
return *((long long*)(&value));
|
||||
}
|
||||
}
|
||||
|
||||
#include <QTextCodec>
|
||||
|
||||
BytesAsString::BytesAsString()
|
||||
:AbstractTranslateUnit(NAME(BytesAsString)){
|
||||
_conv_with = QTextCodec::codecForName("GBK");
|
||||
}
|
||||
|
||||
void BytesAsString::setStrCodec(QTextCodec* ins)
|
||||
{
|
||||
this->_conv_with = ins;
|
||||
}
|
||||
|
||||
QString BytesAsString::codecName() const
|
||||
{
|
||||
return this->_conv_with->name();
|
||||
}
|
||||
|
||||
QVariant BytesAsString::parse(const QByteArray& bytes) const
|
||||
{
|
||||
return _conv_with->toUnicode(bytes);
|
||||
}
|
||||
|
||||
void BytesAsString::loadFrom(const QJsonObject& obj)
|
||||
{
|
||||
AbstractTranslateUnit::loadFrom(obj);
|
||||
|
||||
QString codec_name;
|
||||
STRING_PEAK(codec_name);
|
||||
this->_conv_with = QTextCodec::codecForName(codec_name.toLatin1());
|
||||
}
|
||||
|
||||
void BytesAsString::saveTo(QJsonObject& obj) const
|
||||
{
|
||||
AbstractTranslateUnit::saveTo(obj);
|
||||
|
||||
auto codec_name = this->codecName();
|
||||
STRING_SAVE(codec_name);
|
||||
}
|
||||
|
||||
std::shared_ptr<Serializable> BytesAsString::newDefault() const
|
||||
{
|
||||
return std::make_shared<BytesAsString>();
|
||||
}
|
||||
|
|
|
@ -127,7 +127,9 @@ public:
|
|||
std::shared_ptr<Serializable> newDefault() const override;
|
||||
};
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 转换源数据为整形
|
||||
/// </summary>
|
||||
class BytesAsInteger : public AbstractTranslateUnit {
|
||||
private:
|
||||
bool unsigned_mark = false;
|
||||
|
@ -146,10 +148,28 @@ public:
|
|||
void loadFrom(const QJsonObject& obj) override;
|
||||
void saveTo(QJsonObject& obj) const override;
|
||||
std::shared_ptr<Serializable> newDefault() const override;
|
||||
|
||||
};
|
||||
|
||||
#define STRING_PEAK(codec_name) codec_name = obj[NAME(codec_name)].toString();
|
||||
#define STRING_SAVE(codec_name) obj[NAME(codec_name)] = codec_name;
|
||||
/// <summary>
|
||||
/// 转换源数据为字符串
|
||||
/// </summary>
|
||||
class BytesAsString : public AbstractTranslateUnit {
|
||||
private:
|
||||
QTextCodec *_conv_with;
|
||||
QTextCodec *_conv_with = nullptr;
|
||||
|
||||
public:
|
||||
BytesAsString();
|
||||
|
||||
void setStrCodec(QTextCodec *ins);
|
||||
QString codecName() const;
|
||||
|
||||
// TranslateUnit ============================
|
||||
QVariant parse(const QByteArray& bytes) const override;
|
||||
|
||||
// Serializable ==============================
|
||||
void loadFrom(const QJsonObject& obj) override;
|
||||
void saveTo(QJsonObject& obj) const override;
|
||||
std::shared_ptr<Serializable> newDefault() const override;
|
||||
};
|
|
@ -9,10 +9,9 @@ int main(int argc, char *argv[])
|
|||
QApplication app(argc, argv);
|
||||
|
||||
QByteArray vbuf;
|
||||
uint value = 122;
|
||||
uint value = -122;
|
||||
vbuf.append((char*)&value, 4);
|
||||
BytesAsInteger u;
|
||||
u.setUnsignedMark(true);
|
||||
qDebug() << u.parse(vbuf);
|
||||
|
||||
return app.exec();
|
||||
|
|
Loading…
Reference in New Issue