Lazarus

Programming => General => Topic started by: itblumi on November 27, 2022, 11:20:03 pm

Title: Malformed UTF-8 string with special characters
Post by: itblumi on November 27, 2022, 11:20:03 pm
Why do I get an error from Lazarus (2.2.4) with the following example. The character in the following example is in the UTF-8 codepage and I don't understand why I get this message from the compiler. It's the same with other special characters.

Code: Pascal  [Select][+][-]
  1. var
  2.   AStr: string;
  3. begin
  4.   // this char is in the UTF-8 codepage [228    U+00E4  C3 A4   ä      Latin Small Letter A With Diaeresis]
  5.   AStr := 'ä'; //  Error: Malformed UTF-8 string
  6. end;
  7.  

UTF-8 Codepage https://www.charset.org/utf-8 (https://www.charset.org/utf-8)

Edit: I use Lazarus on Windows 7.
Title: Re: Malformed UTF-8 string with special characters
Post by: dsiders on November 28, 2022, 12:38:22 am
Why do I get an error from Lazarus (2.2.4) with the following example. The character in the following example is in the UTF-8 codepage and I don't understand why I get this message from the compiler. It's the same with other special characters.

Code: Pascal  [Select][+][-]
  1. var
  2.   AStr: string;
  3. begin
  4.   // this char is in the UTF-8 codepage [228    U+00E4  C3 A4   ä      Latin Small Letter A With Diaeresis]
  5.   AStr := 'ä'; //  Error: Malformed UTF-8 string
  6. end;
  7.  

UTF-8 Codepage https://www.charset.org/utf-8 (https://www.charset.org/utf-8)

Edit: I use Lazarus on Windows 7.

I don't have a working Windows 7 any more. I tried this using 2.2.4 on Windows 8.1 and Windows 11. Both of the following worked for me.

Code: Pascal  [Select][+][-]
  1. var AStr: String;
  2.  
  3.   AStr:= 'ä';
  4.   AStr := #$c3#$a4;

How did you generate the character in the Editor? Using CharMap or some other mechanism?

Title: Re: Malformed UTF-8 string with special characters
Post by: dbannon on November 28, 2022, 12:41:28 am
itblumi, you code works fine on Linux. That says to me that your issue somehow relates to how Windows handles utf-8 in Lazarus. I am not a Windows user so cannot say what that is, but I don't take extra precautions to make such code work on Windows so I suggest you have a deliberately selected a different code page perhaps ?

Must be a Windows user who can help ...

Davo
Title: Re: Malformed UTF-8 string with special characters
Post by: itblumi on November 28, 2022, 12:48:23 am
Quote
How did you generate the character in the Editor? Using CharMap or some other mechanism?

I used my keyboard for this my default keyboard is set to german, but I have the same issue for characters like this "á". It could be that lazarus or windows create this character
in Ansi, but I'm not sure about this. I will google this for Windows 7.
Title: Re: Malformed UTF-8 string with special characters
Post by: itblumi on November 28, 2022, 01:10:41 am
I think I know the issue. Delphi create the pascal file with ANSI encoding and Lazarus has then an issue with them. I will try to convert the *.pas files to UTF-8 and try them in Delphi and Lazarus.
Title: Re: Malformed UTF-8 string with special characters
Post by: itblumi on November 28, 2022, 01:22:14 am
I could fix the issue to convert all the *.pas files to UTF-8 with BOM.
Thanks for the help!
Title: Re: Malformed UTF-8 string with special characters
Post by: dbannon on November 28, 2022, 01:34:34 am
Ah, yes, BOM. What a great idea !  Lets hide something at the start of a file that completely changes the nature of the file and we'll see how long it takes people to find it !

 :D

Glad you solved your problem itblumi !

Davo
Title: Re: Malformed UTF-8 string with special characters
Post by: itblumi on November 28, 2022, 01:37:31 am
I changed to this format, because Delphi will save the file everytime with BOM  ;)
TinyPortal © 2005-2018