TEncoding

by Radek Červinka 3. April 2011 00:04

Unicode is today standard. Before unicode many programmers needs transform from one codepage to another codepage (sometimes in unicode too).

Very good tool was synapse (there is synacode unit). But in Delphi 2010 (finally) there is native transcoding support in RTL (and I can say good implemented).

There is SysUtils.TEncoding class (and 30K of tables, which increased EXE size). This class introduce some standard codepages (Unicode, UTF8, UTF7, ASCII …) and for others implemented interface. Class is very similar to class in .NET.

In following example shows basic use.

    1program Project1;
    2
    3{$APPTYPE CONSOLE}
    4
    5uses
    6  SysUtils,
    7  Classes;
    8
    9var
   10  enc: TEncoding;
   11  by: TBytes;
   12  target: TBytes;
   13  fw: TBinaryWriter;
   14  sw: TStreamWriter;
   15  sl: TStringList;
   16
   17const
   18  s = 'Příliš žluťoučký kůň úpěl dábelské ódy.';
   19begin
   20//  enc := TEncoding.ASCII; // target codepage
   21//  enc := TEncoding.UTF8; // in this case use UTF8Encode('')
   22//  enc := TEncoding.GetEncoding(852);
   23  enc := TEncoding.GetEncoding('ISO-8859-2');
   24
   25  by := TEncoding.Unicode.GetBytes(s);
   26  target := TEncoding.Convert(TEncoding.Unicode, enc, by);
   27
   28  fw:= TBinaryWriter.Create('data.txt');
   29  fw.Write(target);
   30  fw.Close;
   31
   32  sw:= TStreamWriter.Create('data2.txt', False,  enc);
   33  sw.Write(s);
   34  sw.Close;
   35
   36  sl := TStringList.Create;
   37  sl.Text := s;
   38  sl.SaveToFile('data3.txt', enc);  // + CRLF
   39  sl.Free;
   40
   41end.

There is special Czech sentence with most of special chars for demonstration. In lines 20-23 there are examples of obtaining special codepages. Apart from TEncoding class, there are specials methods in RTL for working with UTF8 and so.

On line 25 we get byte data of our sentence (remember: unicode = string), on line 26 make conversion (parameters:source codepage, targed codepage, data) so now we have converted data.

TBinaryWriter is very nice class (I like it) so we use it for save.

But there is simple option - TStreamWriter have direct support for save data in selected codepage (TStreamWriter for loading). There are another classes with similar extension, for exaple TStringList - see listing.

BTW: now in Delphi there are this classes: Classes.TBinaryWriter (binary data), Classes.TStreamWriter (chars), Classes.TStringWriter (string), Classes.TTextWriter (base class), Classes.TWriter (classic class for object write).

And vice versa for TxxxxReader.

Just for completeness: TEncoding interface has many methods, but basic idea is above..

Tags: ,

How to

Comments are closed