Forum > General

VarToStr

(1/2) > >>

SymbolicFrank:
It seems, that VarToStr returns a 7-bit ASCII string, while everything is UTF-8. Is that correct? And if so, do I need to use VarToUnicodeStr or VarToWideStr instead? It sounds like those return multi-byte chars.

Zvoni:

--- Quote from: SymbolicFrank on November 21, 2022, 03:47:56 pm ---It seems, that VarToStr returns a 7-bit ASCII string, while everything is UTF-8. Is that correct? And if so, do I need to use VarToUnicodeStr or VarToWideStr instead? It sounds like those return multi-byte chars.

--- End quote ---
Depends which OS:
https://www.freepascal.org/docs-html/rtl/system/widestring.html
https://www.freepascal.org/docs-html/rtl/system/unicodestring.html

--- Quote ---UnicodeString is a string of WideChars. The main difference with WideString is that unicodestring is reference counted, and WideString is not reference counted on Windows.

--- End quote ---

But shouldn't {$H+} switch to UTF8 in any way?
VarToStr returns "String" so that should be UTF8-"compatible" if {$H+} is on
https://www.freepascal.org/docs-html/prog/progsu25.html
Which mode are you in?

--- Quote --- a {$MODE DELPHI} directive implies a {$H+} statement, all other modes switch it off.
--- End quote ---

Thaddy:
{$H+} by it self just makes it AnsiString, NOT utf8string. But in Lazarus (not fpc!) it will be treated as UTF8.
Reason being that UTF8 is not a native Freepascal type. So be careful with console/terminal programs.
Although for Ansi there is Ansi code page CP_UTF8. Also in FPC there is Utf8String, but as I wrote that is not a native Freepascal string type.

SymbolicFrank:
I'll try if I can find where exactly it happens, but "coupé" ends up being "coupe", after multiple conversions to/from variant to string with VarToString and vice versa (by assignment). The thing is, I don't always know the exact data type it contains, so when assigning it to a string, I always convert it up front. Just to be sure. Both files where the assignments happen are "{$mode Delphi}{$H+}". It is Windows 10 / trunk.

Thaddy:
If you can avoid variants, please do so.

Navigation

[0] Message Index

[#] Next page

Go to full version