The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,262 other subscribers

Chr equivalent for Unicode in Delphi 7 – Stack Overflow

Posted by jpluimers on 2020/06/17

From a long time ago: [WayBackchr equivalent for Unicode in Delphi 7 – Stack Overflow answered by David Heffernan:

Q

I need to initialize a Widestring in Delphi 7 but I can’t use chrfunction which is ANSI

var
  ws : Widestring;
begin

 ws := chr($FFFF) + chr($FFFF) + chr($FFFF);

end;

What can I use, then ?

A

I’m not sure there’s a simply way to do what you wish. You can convert a Word into a WideChar with a simple cast:

WideChar($FFFF)

but you cannot concatenate WideChar. So this is a compiler error:

WideChar($FFFF) + WideChar($FFFF)

You could use a helper function to get the job done:

function InitialiseWideString(const chars: array of Word): WideString;
var
  i: Integer;
begin
  SetLength(Result, Length(chars));
  for i := 0 to high(chars) do
    Result[i+1] := WideChar(chars[i]);
end;

Then you can call it like this:

ws := InitialiseWideString([$0054, $0069, $006D, $0065, $0020, $0074, $006F, 
  $0020, $0075, $0070, $0067, $0072, $0061, $0064, $0065]);

–jeroen

3 Responses to “Chr equivalent for Unicode in Delphi 7 – Stack Overflow”

  1. xepol said

    WideChar typecasting from a word is how it is done all over the RTL, so I would guess that is the closest way to correct, unless you need a utf32 code point that creates a mbcs utf16 sequence. (it’s in the standard, not sure how common it is)

  2. Arnaud Bouchez said

    What about the following?

    ws := #$FFFF#$FFFF#$FFFF;

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.