Forum > LazUtils

[solved] Converting UnicodeString to Codepage?

(1/2) > >>

Aidex:
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!

lucamar:
In package LazUtils, unit LazUnicode, function UnicodeToWinCP()

PascalDragon:

--- Quote from: lucamar on June 24, 2020, 04:16:32 pm ---In package LazUtils, unit LazUnicode, function UnicodeToWinCP()

--- End quote ---

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


--- Quote from: 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!

--- End quote ---

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)

ASerge:

--- Quote from: 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)

--- End quote ---

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function UnicodeToAnsiString(const S: UnicodeString; CodePage: TSystemCodePage): AnsiString;begin  Result := '';  widestringmanager.Unicode2AnsiMoveProc(Pointer(S), RawByteString(Result), CodePage, Length(S));end;

Aidex:
Thanks for the answers!!!  :)
I'll give the WideStringManager a try.

Navigation

[0] Message Index

[#] Next page

Go to full version