Recent

Author Topic: [solved] Converting UnicodeString to Codepage?  (Read 514 times)

Aidex

  • Jr. Member
  • **
  • Posts: 64
[solved] Converting UnicodeString to Codepage?
« 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!
« Last Edit: June 24, 2020, 05:16:05 pm by Aidex »

lucamar

  • Hero Member
  • *****
  • Posts: 3084
Re: Converting UnicodeString to Codepage?
« Reply #1 on: June 24, 2020, 04:16:32 pm »
In package LazUtils, unit LazUnicode, function UnicodeToWinCP()
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

PascalDragon

  • Hero Member
  • *****
  • Posts: 2268
  • Compiler Developer
Re: Converting UnicodeString to Codepage?
« Reply #2 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)

ASerge

  • Hero Member
  • *****
  • Posts: 1682
Re: Converting UnicodeString to Codepage?
« Reply #3 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;
« Last Edit: June 24, 2020, 04:44:04 pm by ASerge »

Aidex

  • Jr. Member
  • **
  • Posts: 64
Re: Converting UnicodeString to Codepage?
« Reply #4 on: June 24, 2020, 04:45:41 pm »
Thanks for the answers!!!  :)
I'll give the WideStringManager a try.

lucamar

  • Hero Member
  • *****
  • Posts: 3084
Re: Converting UnicodeString to Codepage?
« Reply #5 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 :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Aidex

  • Jr. Member
  • **
  • Posts: 64
Re: [solved] Converting UnicodeString to Codepage?
« Reply #6 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