Lazarus

Free Pascal => Windows => Topic started by: Vampirro on December 06, 2016, 08:49:06 am

Title: Unit CRT changes the ASCII character set
Post by: Vampirro on December 06, 2016, 08:49:06 am
Hi,

This is my first post and I'm sorry about my poor English.

Well, I have to make an old fashioned DOS application for Windows, with the ▒╣ and similar characters to create windows. But when I use the CRT unit for using colours, gotoxy and so on the character set changes to CP-1252. I've tried to print all the ASCII character set but these characters (▒╣╩╦) have gone. I've tried to use UTF-8 with

Code: Pascal  [Select][+][-]
  1. write (utf8ToAnsi('░'));

But the result is the character '¦' (the same with ▒,▓...)

How can I resolve this problem? Thanks for all
Title: Re: Unit CRT changes the ASCII character set
Post by: Vampirro on December 06, 2016, 12:05:53 pm
I don't know if it's important, but... I have Windows 10. This code:

Code: Pascal  [Select][+][-]
  1. program ascii;
  2.  
  3. begin
  4.     writeln(chr(176));
  5. end.
  6.  

prints "░" in screen, but this one:

Code: Pascal  [Select][+][-]
  1. program ascii;
  2.  
  3. uses crt;
  4.  
  5. begin
  6.     writeln(chr(176));
  7. end.
  8.  

prints this character "°". Why? How can I continue using the "non-CRT" characters with the crt functions and procedures?
Title: Re: Unit CRT changes the ASCII character set
Post by: marcov on December 06, 2016, 01:06:38 pm
Windows has three character encodings:

- Unicode, which is UTF16 for unicode characters
- ansi, which is a collective name for various 8-bit encodings (including utf-8 in the few places where Windows uses it)
- oem, the dos-like codepage from the console.

Constants are ansi, but you want to print OEM. The conversion function is ansitooem (which is nowadays implemented using chartooem (https://msdn.microsoft.com/en-us/library/windows/desktop/dd319646(v=vs.85).aspx)). CRT takes over the console and probably does this automatically (?)

Another thing to try is changing the OEM codepage from the default 850 to ancient default dos with

chcp 437

before running your program.
Title: Re: Unit CRT changes the ASCII character set
Post by: Vampirro on December 06, 2016, 07:04:18 pm
Thanks!

But... I'm not used to code in Pascal. I'm looking for an example of CharToOemBuff function in a Free Pascal program but I cannot find it. Could you give me an example, please?

Thanks for all
Title: Re: Unit CRT changes the ASCII character set
Post by: jackokring on November 15, 2018, 08:15:47 pm
https://bitbucket.org/jackokring/clifly-fpc/src/master/ Not all of it, as it is far from complete U437 does translations from ANSI code page and cp437 into UTF-8 encoded AnsiStrings. If your terminal is UTF-8 compatible, then this should print correct. It seems the console appears to switch into ISO latin-1, which indicates potential Unicode compatibility? I've tried the source, but the code page mentioned in crt, video ... confusion.
Title: Re: Unit CRT changes the ASCII character set
Post by: lucamar on November 15, 2018, 09:28:59 pm
The most probable culprit is crt.CrtWrite(). It starts with:

Code: Pascal  [Select][+][-]
  1.   OldConsoleOutputCP:=GetConsoleOutputCP;
  2.   SetConsoleOutputCP(GetACP);
  3.  

That switchs (if at all possible) the console to the current Windows code-page and IIRC, no "normal" Windows (aka ANSI) codepage contains those funny DOS characters.
Title: Re: Unit CRT changes the ASCII character set
Post by: engkin on November 16, 2018, 05:03:42 am
See if UnicodeCRT (http://forum.lazarus.freepascal.org/index.php/topic,42064.msg294106.html#msg294106) works for you.

Edit:
Sorry, I fell in the trap of responding to an old thread.
Title: Re: Unit CRT changes the ASCII character set
Post by: Weosule on February 16, 2019, 02:15:53 am
Hi...

I have had similar problems when recompiling programs I wrote in Turbo Pascal 7 for DOS. However, I discovered that FP has a number of units to cover the problem. Unit cp437 (Standard ASCII) is probably the unit you need to resolve your problem, however, there are units for other DOS and Windows code pages. Additional information can be found in the PDF file on runtime units in the document section of this site. You may have to use the actual character codes, e.g. chr(246) for lower case o-umlaut, for any characters in hi-ASCII.

Hope this helps,

Weosule
TinyPortal © 2005-2018