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

silvioprog silvioprog at gmail.com
Wed Dec 23 17:46:09 CET 2015


On Wed, Dec 23, 2015 at 6:35 AM, Lubos Pintes <lubos.pintes at gmail.com>
wrote:

> Hello,
> I have a text in the WideString variable. I want to send this text to a
> SAPI5 synthesizer. I am doing this as follows:
> WideStr := Format('<pitch absmiddle="%d">%s</pitch>', [Pitch, Text]);

[...]


Assuming that you tested it on Windows, you can't copy a AnsiString (FPC
Format() is ansi on Windows) directly by pointing it to a WideString (your
wide variable), and probably the compiler warns it with an 'Implicit string
type conversion from "AnsiString" to "WideString"'. So, try it with a
string type cast, something like this:

WideStr := WideString(Format('<pitch absmiddle="%d">%s</pitch>', [Pitch,
Text]));

Or use the SysUtils.WideFormat() function, that is wide:

WideStr := WideFormat('<pitch absmiddle="%d">%s</pitch>', [Pitch, Text]);

Just for testing, I did it here and it worked fine (FPC 3.0):

uses
  SysUtils;

var
  WideStr: WideString;
begin
  WideStr := WideString(Format('<pitch absmiddle="%d">%s</pitch>', [123,
'abc']));
  WriteLn('String: ', WideStr);
  WriteLn('Length: ', Length(BytesOf(WideStr)));
  WideStr := '';
  WideStr := WideFormat('<pitch absmiddle="%d">%s</pitch>', [123, 'abc']);
  WriteLn('String: ', WideStr);
  WriteLn('Length: ', Length(BytesOf(WideStr)));
end.

Result:

String: <pitch absmiddle="123">abc</pitch>
Length: 34
String: <pitch absmiddle="123">abc</pitch>
Length: 34

However it is interesting if you send a minimal test program showing the
entire problem and informing your SO, as the guys said for you.

-- 
Silvio Clécio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20151223/7f0c6bd3/attachment.html>


More information about the fpc-pascal mailing list