Recent

Author Topic: How to display non ASCII characters in Lazarus console?  (Read 6075 times)

Blancheflor

  • Newbie
  • Posts: 4
How to display non ASCII characters in Lazarus console?
« on: October 18, 2014, 07:21:58 pm »
Hello everyone, I'm new to Lazarus and Free Pascal, I need your help!
I used first the FPC editor (without Lazarus) which can display without problems my accents in the console but when I changed to Lazarus, all the non-ASCII characters are displayed strangely in the console. I tried to understand how to fix it but the wiki is too technical and the examples in the forum don't work for me or I don't understand what I should try from the code examples. Here is what I wrote in Lazarus:
Code: [Select]
program Project1;
var
  Txt1 : widestring;
  Txt2 : Utf8String;
begin
  txt1 :='é';
  txt2 :='à';
  writeln(txt1);
  writeln;
  writeln(txt2);
  readln();
end.
                 

But the console displays 2 strange characters instead. Can someone tell me how to correct this program to display these characters in the console? I'm using the latest Lazarus(32bits)/FPC versions and Windows 7 64 bits. Thanks in advance for your help! :)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to display non ASCII characters in Lazarus console?
« Reply #1 on: October 18, 2014, 08:07:25 pm »
Make sure the LCL is added as a dependency for your project and try this:

Code: [Select]
program Project1;

{$mode objfpc}{$H+}

uses LazUTF8;

var
  Txt1 : string;
  Txt2 : string;
begin
  txt1 :='éééééééééé';
  txt2 :='àààààààààà';
  writeln(UTF8ToConsole(Txt1));
  writeln;
  writeln(UTF8ToConsole(Txt2));
  readln();
end.
« Last Edit: October 18, 2014, 10:27:43 pm by howardpc »

Blancheflor

  • Newbie
  • Posts: 4
Re: How to display non ASCII characters in Lazarus console?
« Reply #2 on: October 18, 2014, 08:52:54 pm »
Thank you Howard, it works, here's a magic chocolate box for you! :)

if chocolatepieces := 0 then chocolatepieces := +1;

As usual I copy-pasted the code without thinking and it gave me a dependency error message, then I saw your line and found the dependency and added it*. This was the only thing annoying me in the console so I can continue to learn the basics.

*If you are stuck here too, click Project in the menu bar then Project Inspector/Add/New Condition, in Package Name drop down list type "LazUtils" then click Create New Requirement.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: How to display non ASCII characters in Lazarus console?
« Reply #3 on: October 19, 2014, 12:29:35 am »
if chocolatepieces := 0 then chocolatepieces := +1;

That won't compile  O:-)

Bart

Blancheflor

  • Newbie
  • Posts: 4
Re: How to display non ASCII characters in Lazarus console?
« Reply #4 on: October 19, 2014, 04:31:57 am »
if chocolatepieces := 0 then chocolatepieces := +1;

That won't compile  O:-)

Bart

Ok, for you then, this took me ages and almost destroyed my brain %) :

Code: [Select]
program magic_box;
uses
  crt;
var
  choco: integer;
  answer: string;

begin
  choco := 3;
  while choco > 0 do
  begin
  writeln('Your box contains ', choco, '. Do you want to eat a chocolate piece? y/n ');
  readln(answer);
  if answer = 'y' then
  choco := choco -1
  else if answer = 'n' then
  writeln('you hypocrite!');
  end;
  writeln('Your box contains ', choco, ' chocolate pieces :-(');
  writeln;
  writeln('Ok stop crying... Do you believe in magick? y/n' );
  readln(answer);
if answer = 'y' then
begin
choco := choco +1;
  writeln('Your faith in magick has saved your stomach, wait while I summon the delicacy');
   delay (1000);
  write ('Abra...');
   delay (1000);
   write('ca...');
   delay (1000);
   write('da...');
   delay (1000);
   writeln('bra...');
   writeln('Your box contains now ', choco, ' chocolate piece!');
  end
  else if answer ='n' then
  writeln('No chocolate for you then, you unfaithful one!');
  readkey;
 end.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: How to display non ASCII characters in Lazarus console?
« Reply #5 on: October 19, 2014, 11:07:24 am »
That's very good customer service ;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: How to display non ASCII characters in Lazarus console?
« Reply #6 on: October 19, 2014, 05:08:42 pm »
 :D

@Blancheflor:welcome to the forum   :)
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

 

TinyPortal © 2005-2018