Defining a Custom Encoding

Even if Delphi 2009 comes with a few predefined encodings, you might end up needing more. An example of a not-so-common encoding you might need is UTF-32 (little-endian). Defining and using a custom encoding is certainly possible, although there are a few rough edges.

First of all, you have to define a class that inherits from either TEncoding or one of its descendants. Since there are no existing encoding classes handling 4-bytes characters, I've gone for inheriting from the base class: type

TUTF32Encoding = class (TEncoding) class var

UTF32Encoding: TUTF32Encoding; strict protected function GetByteCount(Chars: PChar;

CharCount: Integer): Integer; override; function GetBytes(Chars: PChar;

50 If you are interested to learn about class helpers, a good source is my "Delphi 2007 Handbook", but you can certainly find other references searching the web. The concept of class helper is a little known but extremely powerful feature of recent versions of Delphi.

CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; override; function GetCharCount(Bytes: PByte;

ByteCount: Integer): Integer; override; function GetChars(Bytes: PByte; ByteCount: Integer; Chars: PChar; CharCount: Integer): Integer; override; publ ic function GetPreamble: TBytes; override; class function Encoding: TEncoding; function GetMaxByteCount(

CharCount: Integer): Integer; override; function GetMaxCharCount(

ByteCount: Integer): Integer; override;

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment