Recent

Author Topic: How do i influence the encoding of write?  (Read 2610 times)

Pascal

  • Hero Member
  • *****
  • Posts: 932
How do i influence the encoding of write?
« on: October 15, 2018, 10:09:25 am »
My prog runs in a cmd which codepage is set to 28591 (ISO-8859-1 aka latin1).
I am not able to output 'äöüßÄÖÜ' with write. I tried different convert funtions with no success.

What am i doing wrong here? Any ideas?
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

Thaddy

  • Hero Member
  • *****
  • Posts: 14166
  • Probably until I exterminate Putin.
Re: How do i influence the encoding of write?
« Reply #1 on: October 15, 2018, 11:29:19 am »
Well, if you wrote it as a console program the ansistring is CP_ACP encoded. That is your default for the system.
If you wrote it in Lazarus it may be UTF8. In that case try:
Code: Delphi  [Select][+][-]
  1. type
  2.   cp8891String = type Ansistring(8859);
  3.  
  4. var
  5.   writeme:cp8859String;
  6. // and subsequently assign the string to writeme...
  7.  
« Last Edit: October 15, 2018, 11:32:58 am by Thaddy »
Specialize a type, not a var.

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: How do i influence the encoding of write?
« Reply #2 on: October 15, 2018, 12:04:26 pm »
You need to set code page of Output text file, so your string would be shown normally at the console prompt.

Code: Pascal  [Select][+][-]
  1. SetTextCodePage(Output, 8859);

https://www.freepascal.org/docs-html/rtl/system/settextcodepage.html

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: How do i influence the encoding of write?
« Reply #3 on: October 15, 2018, 12:25:02 pm »
Thanks!

But both suggestions didn't do the job.
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: How do i influence the encoding of write?
« Reply #4 on: October 15, 2018, 12:46:27 pm »
Write seems to always output utf8 as there are two characters per umlaut.
If i use UTF8To... functions this gets reduced to 1 character but it's never the right one!
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: How do i influence the encoding of write?
« Reply #5 on: October 15, 2018, 01:02:39 pm »
Strange! I got it working by:

Code: Pascal  [Select][+][-]
  1. SetTextCodePage(Output, 65001); // utf-8, so there should be no conversion on write
  2. Write(UTF8ToCP850('äöüßÄÖÜ'));

---

If i query the codepage at the start of my prog by
Code: Pascal  [Select][+][-]
  1. GetTextCodePage(Output)
it outputs 28591.

So it should have converted my strings automatically already, which was not the case!
Is this a bug?

---

EDIT: While this works on a console on Windows Server 2008 R2, it does not on Windows 10 (1803).

---

EDIT 2: For Windows 10 it's working with:
Code: Pascal  [Select][+][-]
  1. Write(UTF8ToISO_8859_1('äöüßÄÖÜ', True));
But this does not work on Windows Server 2008 R2!

---

EDIT 3: 2008 R2 also works with:
Code: Pascal  [Select][+][-]
  1. SetTextCodePage(Output, 850);
  2. Write(UTF8ToIso_8859_1('äöüßÄÖÜ', True));

« Last Edit: October 15, 2018, 02:05:09 pm by Pascal »
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: How do i influence the encoding of write?
« Reply #6 on: October 15, 2018, 04:41:38 pm »
If i query the codepage at the start of my prog by
Code: Pascal  [Select][+][-]
  1. GetTextCodePage(Output)
it outputs 28591.

So it should have converted my strings automatically already, which was not the case!
Is this a bug?

If i add
Code: Pascal  [Select][+][-]
  1. {$codepage utf8}
to the unit, it in fact gets converted automatically.

So cmd.exe of Servr 2008 R2 seems to have a bug. Can anyone confirm this?

---

Which codepage is used wihtout {$codepage utf8}?
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

 

TinyPortal © 2005-2018