Begin

function TUTF32Encoding.GetMaxCharCount(

ByteCount: Integer): Integer; begi n

Result := (ByteCount div 4) + (ByteCount and l) + l; end;

function TUTF32Encoding.GetPreamb1e: TBytes; begi n

// UTF-32, little-endian SetLength(Resu1t, 4); Resu1t[0] := $FF Resu1t[l] := $FE Resu1t[2] := $00 Resu1t[3] := $00 end;

The code mimics somewhat the Delphi RTL classes, resembling in particular the TUnicodeEncoding class. The conversion methods are slightly more complicated. Since I don't want to handle the characters directly, I'm using the UnicodeString to UCS4String conversion functions provided by the Delphi RTL. Extracting the physical bytes to store in the stream is a matter of converting to UCS4String and moving the data at a low level:

function TUTF32Encoding.GetBytes(Chars: PChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; var u4String: UCS4String; begi n

Result := CharCount * SizeOf(UCS4Char); u4String := UnicodeStringToUCS4String (

UnicodeString (Chars)); Move(u4String[0], BytesA, Result); end;

For the opposite conversion you need to move the binary data to the UCS4String type, convert it, and copy the result in the output buffer:

function TUTF32Encoding.GetChars(Bytes: PByte; ByteCount: Integer; Chars: PChar; CharCount: Integer): Integer; var u4String: UCS4String; uString: UnicodeString; begi n

Result := CharCount; SetLength (u4String, Result); Move(BytesA, u4String[0],

CharCount * SizeOf(UCS4Char)); uString := UCS4StringToUnicodeString (u4String);

Move(uString[l], CharsA, CharCount * SizeOf(Char));

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