Recent

Author Topic: [Solved] Accented letters in a TStringlist  (Read 3308 times)

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 586
    • Double Dummy Solver - free download
[Solved] Accented letters in a TStringlist
« on: March 22, 2018, 11:06:57 pm »
With a TStringlist I LoadFromFile some data that includes accented letters. For example: Gérard

this shows up as: G?rard  How can I retrieve and store this info with accented letters.
« Last Edit: March 23, 2018, 04:44:26 pm by bobonwhidbey »
Lazarus 3.0RC2, FPC 3.2.2 x86_64-win64-win32/win64

RayoGlauco

  • Full Member
  • ***
  • Posts: 174
  • Beers: 1567
Re: Accented letters in a TStringlist
« Reply #1 on: March 22, 2018, 11:41:06 pm »
I use tStringList, LoadFromFile and SaveToFile in my programs and I have no problems with accented characters.
Maybe you should rather examine what you do to create your file, and what you're doing with the strings after you load them from that file.
There could be a problem in the file encoding, or in the function that shows the information.
For example, if your file text is encoded as ANSI and you handle it as UTF8, you will get bad characters.
« Last Edit: March 23, 2018, 12:27:24 am by RayoGlauco »
To err is human, but to really mess things up, you need a computer.

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 586
    • Double Dummy Solver - free download
Re: Accented letters in a TStringlist
« Reply #2 on: March 23, 2018, 12:16:44 am »
Here's what I've done:
Code: Pascal  [Select][+][-]
  1. var
  2.   InputData : TStringList;
  3. begin
  4.     InputData := TStringList.Create;
  5.     InputData.LoadFromFile(f);
  6.     s := InputData[12];  // in the file, line 12 has "Gérard"

but s (on Laz Watches) shows up as "G?rard"

Nothing is being done between loading the file and displaying the contents of the line.
Lazarus 3.0RC2, FPC 3.2.2 x86_64-win64-win32/win64

RayoGlauco

  • Full Member
  • ***
  • Posts: 174
  • Beers: 1567
Re: Accented letters in a TStringlist
« Reply #3 on: March 23, 2018, 12:35:31 am »
I think your file is encoded as ANSI. You may need to use ANSItoUTF8() or some function like this.

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

You can test this:

Code: Pascal  [Select][+][-]
  1.     var
  2.       InputData : TStringList;
  3.     begin
  4.         InputData := TStringList.Create;
  5.         InputData.LoadFromFile(f);
  6.         s := ANSItoUTF8(InputData[12]);  // in the file, line 12 has "Gérard"
« Last Edit: March 23, 2018, 12:53:43 am by RayoGlauco »
To err is human, but to really mess things up, you need a computer.

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Accented letters in a TStringlist
« Reply #4 on: March 23, 2018, 12:37:55 am »
@bobonwhidbey

There are many solutions, I have attached a solution for Microsoft Windows using Notepad++ as one example of converting a file format from ANSI to UTF8.

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 586
    • Double Dummy Solver - free download
Re: Accented letters in a TStringlist
« Reply #5 on: March 23, 2018, 01:29:13 am »
This didn't work for me:
 s := ANSItoUTF8(InputData[12]);

Converting the entire file with Notepad++ worked fine.  Is there a way to convert the file from within Laz, at runtime, rather than using another program?
Lazarus 3.0RC2, FPC 3.2.2 x86_64-win64-win32/win64

ptvs

  • Newbie
  • Posts: 6
Re: Accented letters in a TStringlist
« Reply #6 on: March 23, 2018, 06:51:35 am »
If the file is not a large one, try this:
Code: Pascal  [Select][+][-]
  1.   InputData.Text := AnsiToUtf8(InputData.Text);

balazsszekely

  • Guest
Re: Accented letters in a TStringlist
« Reply #7 on: March 23, 2018, 07:10:20 am »
Try something like this:
Code: Pascal  [Select][+][-]
  1. uses LConvEncoding;
  2.  
  3. function LoadFromFile(const AFileName: String): String;
  4. var
  5.   Ms: TMemoryStream;
  6.   Str: String;
  7. begin
  8.   Ms := TMemoryStream.Create;
  9.   try
  10.     Ms.LoadFromFile(AFileName);
  11.     Ms.Position := 0;
  12.     SetLength(Str, Ms.Size);
  13.     Ms.Read(Pointer(Str)^, Ms.Size);
  14.     Result := ConvertEncoding(Str, GuessEncoding(Str), EncodingUTF8, True);
  15.   finally
  16.     Ms.Free;
  17.   end;
  18. end;
  19.  
  20. procedure TForm1.Button1Click(Sender: TObject);
  21. begin
  22.   Memo1.Text := LoadFromFile('test.txt');
  23. end;        
« Last Edit: March 23, 2018, 07:14:58 am by GetMem »

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 586
    • Double Dummy Solver - free download
Re: Accented letters in a TStringlist
« Reply #8 on: March 23, 2018, 04:44:10 pm »
Thank you GetMem, that worked perfectly.
Lazarus 3.0RC2, FPC 3.2.2 x86_64-win64-win32/win64

 

TinyPortal © 2005-2018