Skip to content

TEncoding

[Properties (by Name)][Methods (by Name)][Events (by Name)]

Encoding support

Declaration

Source position: sysencodingh.inc line 21

Type
  TEncoding = class
  strict private
    TStandardEncoding = (seAnsi,seAscii,seUnicode,seBigEndianUnicode,seUTF7,
                         seUTF8);
    FStandardEncodings : Array[TStandardEncoding] of TEncoding;
    FSystemEncodings : Array of TEncoding;
    FLock : TRTLCriticalSection;
    class function GetANSI : TEncoding;  Static;
    class function GetASCII : TEncoding;  Static;
    class function GetBigEndianUnicode : TEncoding;  Static;
    class function GetDefault : TEncoding;  Static;
    class function GetSystemEncoding : TEncoding;  Static;
    class function GetUnicode : TEncoding;  Static;
    class function GetUTF7 : TEncoding;  Static;
    class function GetUTF8 : TEncoding;  Static;
    class constructor Create;
    destructor Destroy;
  strict protected
    FIsSingleByte : Boolean;
    FMaxCharSize : Integer;
    class procedure FreeEncodings;
    function GetByteCount(Chars: PUnicodeChar; CharCount: Integer) : Integer
                         ;  Virtual;  Abstract;  Overload;
    function GetByteCount(const Chars: TUnicodeCharArray) : Integer
                         ;  Overload;
    function GetByteCount(const Chars: TUnicodeCharArray; 
                         CharIndex: Integer; CharCount: Integer) : Integer
                         ;  Overload;
    function GetByteCount(const S: UnicodeString) : Integer;  Overload;
    function GetByteCount(const S: UnicodeString; CharIndex: Integer; 
                         CharCount: Integer) : Integer;  Overload;
    function GetBytes(Chars: PUnicodeChar; CharCount: Integer; 
                     Bytes: PByte; ByteCount: Integer) : Integer;  Virtual
                     ;  Abstract;  Overload;
    function GetBytes(const Chars: TUnicodeCharArray) : TBytes;  Overload;
    function GetBytes(const Chars: TUnicodeCharArray; CharIndex: Integer; 
                     CharCount: Integer) : TBytes;  Overload;
    function GetBytes(const Chars: TUnicodeCharArray; CharIndex: Integer; 
                     CharCount: Integer; const Bytes: TBytes; 
                     ByteIndex: Integer) : Integer;  Overload;
    function GetBytes(const S: UnicodeString) : TBytes;  Overload;
    function GetBytes(const S: UnicodeString; CharIndex: Integer; 
                     CharCount: Integer; const Bytes: TBytes; 
                     ByteIndex: Integer) : Integer;  Overload;
    function GetCharCount(Bytes: PByte; ByteCount: Integer) : Integer
                         ;  Virtual;  Abstract;  Overload;
    function GetCharCount(const Bytes: TBytes) : Integer;  Overload;
    function GetCharCount(const Bytes: TBytes; ByteIndex: Integer; 
                         ByteCount: Integer) : Integer;  Overload;
    function GetChars(Bytes: PByte; ByteCount: Integer; 
                     Chars: PUnicodeChar; CharCount: Integer) : Integer
                     ;  Virtual;  Abstract;  Overload;
    function GetChars(const Bytes: TBytes) : TUnicodeCharArray;  Overload;
    function GetChars(const Bytes: TBytes; ByteIndex: Integer; 
                     ByteCount: Integer) : TUnicodeCharArray;  Overload;
    function GetChars(const Bytes: TBytes; ByteIndex: Integer; 
                     ByteCount: Integer; const Chars: TUnicodeCharArray; 
                     CharIndex: Integer) : Integer;  Overload;
    function GetAnsiBytes(Chars: PChar; CharCount: Integer) : TBytes
                         ;  Virtual;  Abstract;
    function GetAnsiBytes(const S: string) : TBytes;  Overload;
    function GetAnsiBytes(const S: string; CharIndex: Integer; 
                         CharCount: Integer) : TBytes;  Overload;
    function GetAnsiString(Bytes: PByte; ByteCount: Integer) : string
                          ;  Virtual;  Abstract;
    function GetAnsiString(const Bytes: TBytes) : string;  Overload;
    function GetAnsiString(const Bytes: TBytes; ByteIndex: Integer; 
                          ByteCount: Integer) : string;  Overload;
    function GetCodePage : Cardinal;  Virtual;  Abstract;
    function GetEncodingName : UnicodeString;  Virtual;  Abstract;
  public
    function Clone : TEncoding;  Virtual;
    class function Convert(Source: TEncoding; Destination: TEncoding; 
                          const Bytes: TBytes) : TBytes;  Overload;
    class function Convert(Source: TEncoding; Destination: TEncoding; 
                          const Bytes: TBytes; StartIndex: Integer; 
                          Count: Integer) : TBytes;  Overload;
    class function IsStandardEncoding(AEncoding: TEncoding) : Boolean
                                     ;  Static;
    class function GetBufferEncoding(const Buffer: TBytes; 
                                    var AEncoding: TEncoding) : Integer
                                    ;  Overload;  Static;
    class function GetBufferEncoding(const Buffer: TBytes; 
                                    var AEncoding: TEncoding; 
                                    ADefaultEncoding: TEncoding) : Integer
                                    ;  Overload;  Static;
    class function GetEncoding(CodePage: Integer) : TEncoding;  Overload
                              ;  Static;
    class function GetEncoding(const EncodingName: UnicodeString)
                               : TEncoding;  Overload;  Static;
    function GetMaxByteCount(CharCount: Integer) : Integer;  Virtual
                            ;  Abstract;
    function GetMaxCharCount(ByteCount: Integer) : Integer;  Virtual
                            ;  Abstract;
    function GetPreamble : TBytes;  Virtual;  Abstract;
    function GetString(const Bytes: TBytes) : UnicodeString;  Overload;
    function GetString(const Bytes: TBytes; ByteIndex: Integer; 
                      ByteCount: Integer) : UnicodeString;  Overload;
    CodePage : Cardinal;
    EncodingName : UnicodeString;
    IsSingleByte : Boolean;
    ANSI : TEncoding;
    ASCII : TEncoding;
    BigEndianUnicode : TEncoding;
    Default : TEncoding;
    SystemEncoding : TEncoding;
    Unicode : TEncoding;
    UTF7 : TEncoding;
    UTF8 : TEncoding;
  end
  ;

Description

TEncoding is a mostly abstract class that contains various methods to deal with different encodings in single- and double-byte strings. In practice, one instance of a descendent of this class for each needed encoding can be instantiated and used. The class contains several class methods and properties to create such instances, and has several often-used instances available as class properties, in particular the default encoding.

When using encodings (and in particular TEncoding.Default or TEncoding.ANSI ), if the DefaultSystemCodePage changes, you should call TEncoding.FreeEncodings]() to regenerate the encodings.

Members

Member Type Visibility Description
ANSI Property public Ansi encoding instance
ASCII Property public ASCII encoding instance
BigEndianUnicode Property public Big-endian Unicode (UTF16BE) encoding instance
Clone Method public Clone a TEncoding instance
CodePage Property public Codepage for this encoding
Convert Method public Convert an array of bytes from one encoding to another
Create Method strict private
Default Property public Default codepage
Destroy Method strict private
EncodingName Property public Name of this encoding
FIsSingleByte Field strict protected
FLock Field strict private
FMaxCharSize Field strict protected
FreeEncodings Method strict protected Free all standard encodings
FStandardEncodings Field strict private
FSystemEncodings Field strict private
GetANSI Method strict private
GetAnsiBytes Method strict protected
GetAnsiString Method strict protected
GetASCII Method strict private
GetBigEndianUnicode Method strict private
GetBufferEncoding Method public Attempt to guess the encoding of a buffer
GetByteCount Method strict protected
GetBytes Method strict protected
GetCharCount Method strict protected
GetChars Method strict protected
GetCodePage Method strict protected
GetDefault Method strict private
GetEncoding Method public Get an encoding instance for a given codepage.
GetEncodingName Method strict protected
GetMaxByteCount Method public Returns the maximum number of bytes needed to represent a string
GetMaxCharCount Method public Return the maximum number of characters that can be represented in a number of bytes
GetPreamble Method public Return the BOM Marker used by the encoding
GetString Method public Return a string based on an array of bytes
GetSystemEncoding Method strict private
GetUnicode Method strict private
GetUTF7 Method strict private
GetUTF8 Method strict private
IsSingleByte Property public Is the encoding a single-byte encoding or not ?
IsStandardEncoding Method public Check if the encoding is one of the standard encodings
SystemEncoding Property public
TStandardEncoding Type strict private
Unicode Property public UTF16 encoding instance
UTF7 Property public UTF7 encoding instance
UTF8 Property public UTF8 encoding instance

Inheritance

Class Description
TEncoding Encoding support

See also

Name Description
Cardinal An unsigned 32-bits integer.
Integer A signed 16-bits integer
TEncoding.ANSI Ansi encoding instance
TEncoding.ASCII ASCII encoding instance
TEncoding.BigEndianUnicode Big-endian Unicode (UTF16BE) encoding instance
TEncoding.Default Default codepage
TEncoding.Unicode UTF16 encoding instance
TEncoding.UTF7 UTF7 encoding instance
TEncoding.UTF8 UTF8 encoding instance
TRTLCriticalSection A critical section.
UnicodeString A string consisting of widechars.