Any library with string encoding/decoding support?

MGW mgw at yandex.ru
Mon Jan 20 07:00:28 PST 2014


On Monday, 20 January 2014 at 08:33:09 UTC, ilya-stromberg wrote:
> Do you know any library with string encoding/decoding support? 
> I need more encodings than provides `std.encoding`.

Library to work with Qt.
https://github.com/MGWL/QtE-Qt_for_Dlang_and_Forth


Working with Qt and its QTextCodec class.
---------------------------------------

// Compile:
// ------------------------------
// Linux:    dmd ex1.d qte.d -L-ldl
// Windows:  dmd ex1.d qte.d
// ------------------------------

import qte;                             // Work with Qt
import core.runtime;                    // Parametrs start
import std.stdio;                       // writeln();

int main(string[] args) {
     QApplication app;       // Application
     QTextCodec UTF_8;
     QTextCodec WIN_1251;
     QTextCodec IBM866;
     QString tmpQs;
     QByteArray ba;
     QLabel label;

     // Test load. If '--debug' start with warnings message load 
QtE
     bool fDebug; fDebug = false; foreach (arg; args[0 .. 
args.length])  { if (arg=="--debug") fDebug = true; }

     // Load GUI. fDebug=F disable warnings, T=enable warnings
     int rez = LoadQt( dll.Core | dll.Gui | dll.QtE, fDebug); if 
(rez==1) return 1;

     // Init Qt. Last parametr T=GUI, F=console app
     app = new QApplication; 
(app.adrQApplication())(cast(void*)app.bufObj, 
&Runtime.cArgs.argc, Runtime.cArgs.argv, true);

     // Init insaid coding. All codec Qt QTextCodec
     tmpQs = new QString();
     UTF_8 = new QTextCodec("UTF-8");            // Linux
     WIN_1251 = new QTextCodec("Windows-1251");  // Windows
     IBM866 = new QTextCodec("IBM 866");         // DOS

     // Create string "Hello from Qt" on Rushen
     tmpQs.toUnicode(cast(char*)("<h2>Привет из <font color=red 
size=5>QtE.d</font></h2>".ptr), UTF_8);

     // QLabel
     label = new QLabel(null);
     label.setText(tmpQs); 
label.setAlignment(QtE.AlignmentFlag.AlignCenter); // Write text 
and alignment
     label.resize(300, 130); // Size label

     // Exammple DOS console
     ba = new QByteArray(cast(char*)("Привет из QtE.d - обратите 
внимание на перекодировку в DOS".ptr));  // Это в UTF-8
     tmpQs.toUnicode(cast(char*)ba.data(), UTF_8);  // in Unicode
     version(Windows) {  // window DOS in Windows
         tmpQs.fromUnicode(cast(char*)ba.data(), IBM866);
     }
     version(linux) {    // Linux work UTF-8.
         tmpQs.fromUnicode(cast(char*)ba.data(), UTF_8);
     }
     printf("%s", ba.data());

     label.show();

     return app.exec();
}


More information about the Digitalmars-d-learn mailing list