Lazarus

Programming => Packages and Libraries => LazUtils => Topic started by: Aidex on June 24, 2020, 03:58:54 pm

Title: [solved] Converting UnicodeString to Codepage?
Post by: Aidex on June 24, 2020, 03:58:54 pm
Hi!
Is there a function that converts a UnicodeString directly into an AnsiString with a desired CodePage? (without making a detour via UTF-8)
For backwards compatibility, I need to convert the content of a UnicodeString into the Windows system codepage (Windows.GetACP).
For speed reasons I don't want to convert it to UTF8 first and then do another conversion with SetCodepage().
I use Laz v2.0.8.
Thanks!
Title: Re: Converting UnicodeString to Codepage?
Post by: lucamar on June 24, 2020, 04:16:32 pm
In package LazUtils, unit LazUnicode, function UnicodeToWinCP()
Title: Re: Converting UnicodeString to Codepage?
Post by: PascalDragon on June 24, 2020, 04:34:39 pm
In package LazUtils, unit LazUnicode, function UnicodeToWinCP()

You did see that UnicodeToWinCP takes a String as argument aka it's a UTF-8 string?

Hi!
Is there a function that converts a UnicodeString directly into an AnsiString with a desired CodePage? (without making a detour via UTF-8)
For backwards compatibility, I need to convert the content of a UnicodeString into the Windows system codepage (Windows.GetACP).
For speed reasons I don't want to convert it to UTF8 first and then do another conversion with SetCodepage().
I use Laz v2.0.8.
Thanks!

If you're creating a command line application without using Lazarus units you can simply cast your UnicodeString into an AnsiString (which will be of codepage CP_ACP). If you use Lazarus units however things get more complicated, because then DefaultSystemCodePage (which is what CP_ACP evaluates to) will be set to CP_UTF8, which does not help in this case...

You could try to use TEncoding.Convert in that case... (I did not test this however)
Title: Re: Converting UnicodeString to Codepage?
Post by: ASerge on June 24, 2020, 04:41:39 pm
Hi!
Is there a function that converts a UnicodeString directly into an AnsiString with a desired CodePage? (without making a detour via UTF-8)
Code: Pascal  [Select][+][-]
  1. function UnicodeToAnsiString(const S: UnicodeString; CodePage: TSystemCodePage): AnsiString;
  2. begin
  3.   Result := '';
  4.   widestringmanager.Unicode2AnsiMoveProc(Pointer(S), RawByteString(Result), CodePage, Length(S));
  5. end;
Title: Re: Converting UnicodeString to Codepage?
Post by: Aidex on June 24, 2020, 04:45:41 pm
Thanks for the answers!!!  :)
I'll give the WideStringManager a try.
Title: Re: Converting UnicodeString to Codepage?
Post by: lucamar on June 24, 2020, 04:52:13 pm
You did see that UnicodeToWinCP takes a String as argument aka it's a UTF-8 string?

Oops! You're right. Sorry for the bad advice, Aidex :-[
Title: Re: [solved] Converting UnicodeString to Codepage?
Post by: Aidex on June 24, 2020, 05:16:35 pm
I have tried the WideStringManager, it works well!  :)
Many thanks to all of you again.  :)
TinyPortal © 2005-2018