[fpc-pascal] Assigning string constant to a WideString and the Format function
Lubos Pintes
lubos.pintes at gmail.com
Fri Dec 25 16:59:07 CET 2015
Interesting. I thought that SetString is properly overloaded. Maybe it
is, maybe not. But anyway I fixed the program by using
WideStr := PWideChar(@WideCharArray[Index]);
Thus ignoring badly-working SetString.
Anyway thank for all responses!
Dňa 24. 12. 2015 o 14:23 Michael Van Canneyt napísal(a):
>
>
> 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.
>
> Michael.
> _______________________________________________
> fpc-pascal maillist -
> fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
More information about the fpc-pascal
mailing list