How do I port characters expressed as decimals?
How do I port characters expressed as decimals (#0, #1, #9, #10, #13, #32, #128, #255) in Delphi 7 to Delphi Unicode? Some documents (such as 'Delphi in a Unicode world' from Embarcadero) only says that they should be replaced with the actual character. For example instead of #128 I should use '€'. But what would you do with #0??? Or #9? Or #13?
Update:
It looks like this is a tricky question. Somebody stated 开发者_如何转开发here that all chars under 128 remain unchanged. However, 'Delphi in a Unicode world' states otherwise. So, there is a way to use something like #9 in Delphi XE or porting the code from Delphi 7 to Delphi Unicode involves massive code changes?
The low half of ANSI codes [#0..#127] does not change after conversion to Unicode, so you should not bother about it. The high half [#128..#255] is more complicated. The compiler interprets lines
const s1:AnsiString = #200;
const s2:UnicodeString = #200;
depending on {$HIGHCHARUNICODE} directive
{$HIGHCHARUNICODE OFF} // default setting on Delphi 2009
const s1:AnsiString = #200; // Ord(s1[1]) = $C8 = 200
const s2:UnicodeString = #200; // Ord(s2[1]) depends on Codepage
// on my Win1251 = $418
{$HIGHCHARUNICODE ON}
const s1:AnsiString = #200; // Ord(s1[1]) depends on Codepage
// on my Win1251 = $45
const s2:UnicodeString = #200; // Ord(s2[1]) = $C8 = 200
#200 is 'И' on Win1251 codepage. The Unicode codepoint for 'И' is $418.
On the other hand, in Unicode #200 is 'È'. Win1251 does not have 'È' character and Unicode -> Win1251 conversion transforms it to 'E' which is #$45 for all ANSI codepages.
With {$HIGHCHARUNICODE OFF} the compiler interprets #128..#255 characters as ANSI characters depending on system codepage and, if necessary, converts them to unicode codepoints.
With {$HIGHCHARUNICODE ON} the compiler interprets #128..#255 characters as Unicode codepoints and, if necessary, converts them to ANSI characters with possible loss.
Update
I see now the problem code from the Nick's article that works wrong on Unicode Delphi:
var
Buf: array[0..32] of Char;
begin
FillChar(Buf, Length(Buf), #9);
ShowMessage(Format('%d %d %d', [Ord(Buf[0]), Ord(Buf[0]), Ord(Buf[16])]));
end;
but that is an example of wrong FillChar
procedure usage; FillChar
just fills the destination buffer with bytes (BTW only the first half of Buf in the above example) and ignores all the new Unicode stuff. FillChar
should be renamed by FillBytes now, and forbidden to use a character (#9) for the third argument.
精彩评论