标签:config 发送 ext 算数 can pre 实现 inf RKE
TextMessage.pro
1 QT -= gui 2 3 CONFIG += c++11 console 4 CONFIG -= app_bundle 5 6 # The following define makes your compiler emit warnings if you use 7 # any feature of Qt which as been marked deprecated (the exact warnings 8 # depend on your compiler). Please consult the documentation of the 9 # deprecated API in order to know how to port your code away from it. 10 DEFINES += QT_DEPRECATED_WARNINGS 11 12 # You can also make your code fail to compile if you use deprecated APIs. 13 # In order to do so, uncomment the following line. 14 # You can also select to disable deprecated APIs only up to a certain version of Qt. 15 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 16 17 SOURCES += main.cpp 18 textmessage.cpp 19 20 HEADERS += 21 textmessage.h
textmessage.h
1 #ifndef TEXTMESSAGE_H 2 #define TEXTMESSAGE_H 3 4 #include <QObject> 5 6 class TextMessage : public QObject 7 { 8 Q_OBJECT 9 QString m_type; 10 QString m_data; 11 public: 12 explicit TextMessage(QObject *parent = nullptr); 13 TextMessage(QString type, QString data, QObject* parent = NULL); 14 15 QString type(); 16 int length(); 17 QString data(); 18 19 QString serialize(); 20 bool unserialize(QString s); 21 signals: 22 23 public slots: 24 }; 25 26 #endif // TEXTMESSAGE_H
textmessage.cpp
1 #include "textmessage.h" 2 3 TextMessage::TextMessage(QObject *parent) : QObject(parent) 4 { 5 m_data=""; 6 m_type=""; 7 } 8 9 TextMessage::TextMessage(QString type, QString data, QObject* parent):QObject(parent) 10 { 11 m_type=type.trimmed(); 12 13 m_type.resize(4,‘ ‘); 14 15 m_data=data.mid(0,0xFFFF); 16 17 } 18 19 QString TextMessage::type() 20 { 21 return m_type; 22 } 23 24 int TextMessage::length() 25 { 26 return m_data.length(); 27 } 28 29 QString TextMessage::data() 30 { 31 return m_data; 32 } 33 34 QString TextMessage::serialize() 35 { 36 QString len=QString::asprintf("%X", m_data.length()); 37 38 len.resize(4,‘ ‘); 39 40 return m_type+len+m_data; 41 } 42 43 bool TextMessage::unserialize(QString s) 44 { 45 bool ret=(s.length()>=8); 46 47 if(ret) 48 { 49 QString type=s.mid(0,4); 50 QString len=s.mid(4,4); 51 int length=len.toInt(&ret,16); 52 53 ret=ret&&( (s.length()-8)==length); 54 55 if(ret) 56 { 57 m_type=type; 58 m_data=s.mid(8,length); 59 } 60 } 61 62 return ret; 63 }
main.cpp
#include <QCoreApplication> #include <QDebug> #include "textmessage.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); TextMessage tm("AB","1234567890"); QString message=tm.serialize(); qDebug()<<message; TextMessage tmt; tmt.unserialize(message); qDebug()<<tmt.type(); qDebug()<<tmt.length(); qDebug()<<tmt.data(); return a.exec(); }
结果
标签:config 发送 ext 算数 can pre 实现 inf RKE
原文地址:https://www.cnblogs.com/zhaobinyouth/p/12250671.html