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

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

#include <QDateTime>
#include <QtEndian>


// 将字符数字转换为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)<<stemp;

        return allData;
    }

    //![2][设置V内容]
    void setVData(QByteArray _data){
        this->_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();
    }
};

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

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值