[fpc-pascal] Assigning string constant to a WideString and the Format function

Michael Van Canneyt michael at freepascal.org
Thu Dec 24 14:23:55 CET 2015

On Thu, 24 Dec 2015, Lubos Pintes wrote:

> Hello,
> Here is a minimal program, it doesn't work as I expected either because 
> there is a bug somewhere in the RTL, or, more probably, I misunderstood 
> something.
> Basically, I am reading an UTF-8 encoded text from stdin, and collecting 
> it in a WideChar array. I am assigning UTF-8 string to a WideString, and 
> then collecting chars from it into the mentioned array.
> Then I am using a SetString procedure to recreate the WideString from 
> collected chars.
> Here is a simple program which demonstrates this:
> program a;
> {$mode delphi}
> uses SysUtils;
> var
>   U: UTF8String;
>   S, W: WideString;
> begin
>   U := 'Hello';
>   W := U;
>   SetString(S, @W[1], Length(W));

This is the problem, not the call to Format.

If you do a writeln('S : ',S); you will see that S is already wrong.
Change it to
    SetString(S, @W[1], Length(W)*SizeOf(WideChar));
and all should go well.


More information about the fpc-pascal mailing list