网络传输TLV数据格式的QT实现

TLV数据格式简单实现 ,完善中...

#include 
#include 


// 将字符数字转换为16进制的字符串
inline QString ToString(uchar* pbuffer,int len,QString seplit=" ")
{
    QString sMessage="";
    QString sTemp = "";
    for (int i = 0; i < len; i++)
    {
        sTemp.sprintf("%02X", *(pbuffer+i));
        if (i!=0)
            sTemp = seplit + sTemp;

        sMessage.append(sTemp);
    }
    return sMessage;
}

//+++++++++++++++++++++++++++TLV数据定义+++++++++++++++++++++++++++++++++++++++++
class TLV
{
public:
    TLV(){
        this->_tag = 0x00;
        this->_len = 0;
    }

    ~TLV(){ }

    //![1][获取TLV内容]
    QByteArray getData(){
        QByteArray allData;
        allData.append (_tag);

        //字节序转换
        unsigned int bigLen = qToBigEndian(_len);
        allData.append ((const char*)&bigLen,4);

        allData.append (_vdata);

        QString stemp;
        for (int i = 0; i < allData.count (); i++) {

            stemp += QString::asprintf ("%02X ",(uchar)allData.at (i));

        }
        qDebug()<<"CMD_"+ToHexString(_tag)<_vdata = _data;
    }

    //![3][设置V内容] 字符数字是否补位,false=不补 true= 补
    void setVData(QString _strNum ,bool isPatch = false){
        if (isPatch) {
            //补位
            this->_vdata = QString("%1").arg(_strNum.toInt(),this->_len,10,QLatin1Char('0')).toUtf8();
        }
        else{
            //不补位
            this->_vdata = _strNum.toUtf8();
        }
    }

    //![4][设置Tag号]
    void setTagName(uchar tagID){
        this->_tag = tagID;
    }

    //![5][设置最大长度]
    void setMaxLen(int maxlen){
        this->_len = maxlen;
    }

    //int
private:
    //![将数值转换为字符串]
    QString ToHexString(int nData, const char* frm="%02X")
    {
        QString str;
        str.sprintf(frm, nData);
        return str;
    }

protected:
    uchar          _tag;    //字段标识(1字节)
    unsigned int   _len;    //字段长度(4字节)
    QByteArray     _vdata;  //内容(二进制)
};

//0X30	终端时间	N	14	固定长度	设备时间:YYYYMMDDHHMMSS
class TLV_30 :public TLV
{
public:
    TLV_30(){
        this->setTagName(0x30);
        this->setMaxLen(14);
        //![当前时间]
         QDateTime dt = QDateTime::currentDateTime();
         QString  dd  = dt.toString("yyyyMMddhhmmss");
         this->_vdata = dd.toUtf8 ();
    }
};

class TLV_FA :public TLV
{
public:
    TLV_FA(){
        this->setTagName(0xFA);
        this->setMaxLen(8);

        int t_userID = 999999;

        this->_vdata = QString::asprintf("%08d",t_userID).toUtf8();
    }
};

//0x48返回交易记录数据
class TLV_48 :public TLV
{
public:
    TLV_48(){
        this->setTagName(0x48);
    }

    //![]获取多条记录数据:记录 1|记录 2|记录 3|…|记录 N
    QStringList getRecordsData()
    {
        QString        rec = QString(this->_vdata);
        QStringList reclst = rec.split("|");

        return reclst;
    }
};

class EBase
{
public:
    EBase() {}
protected:
    QByteArray _joinedData;
};

//+++++++++++++++++++++++++++报文命令++++++++++++++++++++++++++++++++++++++++++++
class E8002_Send :public EBase {
public:
    //TODO::add header data
    TLV_30 v30;
    TLV_FA vFA;
public:

    QByteArray getEData(){

        _joinedData.append(v30.getData());
        _joinedData.append(vFA.getData());

        return _joinedData;
    }

    int getAllLen(){
        return this->_joinedData.count();
    }
};

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

你可能感兴趣的:(QT)