Quoted-Printable Encoder/Decoder for Qt
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
quotedprintable.h #ifndef QUOTEDPRINTABLE_H #define QUOTEDPRINTABLE_H #include <QObject> #include <QByteArray> class QuotedPrintable : public QObject { Q_OBJECT public: static QString& encode(const QByteArray &input); static QByteArray& decode(const QString &input); private: QuotedPrintable(); }; #endif // QUOTEDPRINTABLE_H // quotedprintable.cpp #include "quotedprintable.h" QString& QuotedPrintable::encode(const QByteArray &input) { QString *output = new QString(); char byte; const char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; for (int i = 0; i < input.length() ; ++i) { byte = input[i]; if ((byte == 0x20) || (byte >= 33) && (byte <= 126) && (byte != 61)) { output->append(byte); } else { output->append('='); output->append(hex[((byte >> 4) & 0x0F)]); output->append(hex[(byte & 0x0F)]); } } return *output; } QByteArray& QuotedPrintable::decode(const QString &input) { // 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F const int hexVal[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15}; QByteArray *output = new QByteArray(); for (int i = 0; i < input.length(); ++i) { if (input.at(i).toAscii() == '=') { output->append((hexVal[input.at(++i).toAscii() - '0'] << 4) + hexVal[input.at(++i).toAscii() - '0']); } else { output->append(input.at(i).toAscii()); } } return *output; } //Examples The following example will show how to encode and decode an unicode text: #include <QApplication> #include "quotedprintable.h" #include <iostream> int main(int argc, char **argv) { QApplication(argc, argv); QString text ("Some unicode text: \n" \ "Hungarian text: Jó reggelt kívánok!\n" \ "Chinese text: 您 好,世 界!\n" \ "Math operators : ⋐ ⋑ ⋒ ⋓ ⋔ ⋕ ⋖⋗ ⋘ ⋙ ⋚ ⋛ ⋜ ⋝ ⋞ ⋟\n"); std::cout << "=== Original Text ===\n"; std::cout << text.toStdString(); QString encoded = QuotedPrintable::encode(text.toAscii()); std::cout << "\n=== Quoted-Printable Encoded Text ===\n"; std::cout << encoded.toStdString(); QString decoded = QuotedPrintable::decode(encoded); std::cout << "\n\n=== Decoded Text ===\n"; std::cout << decoded.toStdString(); return 0; } The program's output will be: === Original Text === Some unicode text: Hungarian text: Jó reggelt kívánok! Chinese text: 您 好,世 界! Math operators : ⋐ ⋑ ⋒ ⋓ ⋔ ⋕ ⋖⋗ ⋘ ⋙ ⋚ ⋛ ⋜ ⋝ ⋞ ⋟ === Quoted-Printable Encoded Text === Some unicode text: =0A Hungarian text: J=C3=B3 reggelt k=C3=ADv=C3=A1nok!=0A Chinese text: =E6=82=A8 =E5=A5=BD=EF=BC=8C=E4=B8=96 =E7=95=8C=EF=BC=81=0A Math operators : =E2=8B=90 =E2=8B=91 =E2=8B=92 =E2=8B=93 =E2=8B=94 =E2=8B=95 =E2=8B=96=E2=8B=97 =E2=8B=98 =E2=8B=99 =E2=8B=9A =E2=8B=9B =E2=8B=9C =E2=8B=9D =E2=8B=9E =E2=8B=9F=0A === Decoded Text === Some unicode text: Hungarian text: Jó reggelt kívánok! Chinese text: 您 好,世 界! Math operators : ⋐ ⋑ ⋒ ⋓ ⋔ ⋕ ⋖⋗ ⋘ ⋙ ⋚ ⋛ ⋜ ⋝ ⋞ ⋟ |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=494