Jump to content
jesu

restore ansi from utf8

Recommended Posts

Hello. Since I upgraded to WIndows 11 I've had some text files screwed. Unfortunately, we still need to use ANSI in some files but sometimes (likely by notepad) that is replaced with UTF8. I've created a procedure like this to restore them (I don't know it these codes will be messed in the forum):


 

procedure TFprincipal.ArreglarAcentos;  
const
  ka_AcentosBien : array[1..14] of string =                                    
    ( 'á', 'é', 'í', 'ó', 'ú', 'Á', 'É', 'Ó', 'Ú', 'ñ', 'Ñ', 'ü', 'Ü', 'Í'
    );
  ka_AcentosMal : array[1..14] of string =                                   
    ( 'á', 'é', 'í', 'ó', 'ú', '�', 'É', 'Ó', 'Ú', 'ñ', 'Ñ', 'ü', 'Ü', 'Ì'
    );

var
  i: integer;
  vb_encontrado: boolean;
  
begin
  vb_encontrado := false;
  for i := Low(ka_AcentosMal) to High(ka_AcentosMal) do
  begin
    if (pos(ka_AcentosMal[i], mystring) > 0) then
    begin
//      ProcDebug('encontrado:  ' + ka_AcentosMal[i] + ' reemplazo: ' + ka_AcentosBien[i]);
      vb_encontrado := true;
      mystring := StringReplace(mystring, ka_AcentosMal[i], ka_AcentosBien[i], [rfReplaceAll]);
    end
    else
    begin
//      ProcDebug('no encontrado:  ' + ka_AcentosMal[i]);
    end;
  end;
end;

 

This procedure seems to work well except for i uppercase accented, which in utf8 is c3 + 8d. It seems that Delphi does something special with character 8d and my search doesn't work. How should I do it? 

Thanks.

 

Share this post


Link to post

Yes, the forum messed some characters like:

Á -> c3 81
í -> c3 ad

 

 

Share this post


Link to post

Isn't the root problem where you read these strings in the wrong way and shouldn't it be handled right there?

  • Like 2

Share this post


Link to post

Windows doesn't just arbitrarily screw up files. You must have done something to cause the files to be screwed up, ie loading them or saving them with the wrong charset. You need to use the proper charset when saving/loading files. That's where you need to fix the problem, not in the code that has already loaded the files, by then the data is already corrupted. If you have ANSI files, load them with an ANSI charset. If you have UTF-8 files, load them as UTF-8. Period. If you need to differentiate, use a BOM or other metadata, or hieristic analysis. Don't guess the encoding.

Share this post


Link to post

My advice is to understand a problem before looking for a solution. At the moment it's clear that the problem still eludes you. Concentrate on that first. 

Share this post


Link to post

(P.S.: I refer to Windows OS).

 

First of all, take care that what you write in Delphi IDE may be in Ansi or UTF and depend on this your characters my be misunderstanding after read from files (in the laste release of Delphi those things work better).

 

Second, string type in Delphi is equivalent to Unicode string (UTF-16). Normally the compiler does all the conversions needed, but in same cases it cannot.

 

Look this for you convenience: https://docwiki.embarcadero.com/RADStudio/Athens/en/String_Types_(Delphi)

 

Look better at you characters encoding: c3 8d may not be the exact character did you exepect:

 

368833445_Screenshot2025-06-09112225.thumb.png.3c1c4e5a97d07d3cd6eedc82977ca299.png

 

Edited by DelphiUdIT

Share this post


Link to post

Yes, Windows 11 Notepad screws files. This never happened in Windows 10 after many years using it. You open a file double-clicking it, edit it and just click save expecting that it respects your encoding. Sometimes it does, sometimes not. Sure, you can use Save as to be sure that it uses the encoding you want, but that was never neccessary before.

Just to be clear, I'm not talking about Delphi files. 

 

The fact is that we need to restore these files without loosing time doing it by hand. I've used the table in this page to type the characters in my procedure:

https://www.utf8-chartable.de/

where I see that 

Quote

c3 8d    LATIN CAPITAL LETTER I WITH ACUTE

 

Should I encode ka_AcentosMal in some other way?

Share this post


Link to post
5 minutes ago, jesu said:

The fact is that we need to restore these files without loosing time doing it by hand.

Load the file with UTF-8 encoding and save it with ANSI encoding.

uses
  System.IOUtils;
  
...
  TFile.WriteAllText(FileName, TFile.ReadAllText(FileName, TEncoding.UTF8), TEncoding.ANSI);

If the files are too large to fit into memory, you need to work with different file names for input and output:

  var writer := TStreamWriter.Create(NewFileName, False, TEncoding.ANSI);
  try
    var reader := TStreamReader.Create(FileName, TEncoding.UTF8);
    try
      while not reader.EndOfStream do
        writer.WriteLine(reader.ReadLine);
    finally
      reader.Free;
    end;
  finally
    writer.Free;
  end;

 

Share this post


Link to post
1 hour ago, Uwe Raabe said:

Load the file with UTF-8 encoding and save it with ANSI encoding. 

Why someone would do this ? Depends on places where you do it (I mean OS, LANGUAGE, ....) you will have differents results.

Share this post


Link to post
11 minutes ago, DelphiUdIT said:

Why someone would do this ?

Because it is the reverse of what Notepad in Windows 11 did to the files.

Share this post


Link to post
1 hour ago, jesu said:

Yes, Windows 11 Notepad screws files. This never happened in Windows 10 after many years using it. You open a file double-clicking it, edit it and just click save expecting that it respects your encoding. Sometimes it does, sometimes not. Sure, you can use Save as to be sure that it uses the encoding you want, but that was never neccessary before.

Just to be clear, I'm not talking about Delphi files. 

 

The fact is that we need to restore these files without loosing time doing it by hand. I've used the table in this page to type the characters in my procedure:

https://www.utf8-chartable.de/

where I see that 

 

Should I encode ka_AcentosMal in some other way? 

I don't know why that page present those data. but in many "converters" and also looking at Unicode BMP, surrogate and extended this combination is not valid.

This is  the right coding (with chinese char for test, all confirmed with UTF online services):

 

image.thumb.png.f72116ab43a24dd60dd0a186677eeff8.png

Share this post


Link to post
27 minutes ago, Uwe Raabe said:

Because it is the reverse of what Notepad in Windows 11 did to the files.

Really? My notepad does not and has never done such an operation on its own, until I force it

It reads a file with an encoding and saves it with the same encoding. And the only real usable option is to convert a file in ANSI to Unicode (UTF-xx) and not the "other way around".
Converting a data that can have thousands (at least) of combinations into a data that can only have 256 makes no sense.
In these cases you only work with ANSI encoding without any conversion (as in the case of iterations with old industrial systems or very old equipment).
Taking into account that those who use Delphi normally develop for a multitude of "clients" (I mean develop applications that can be used in various environments) thinking of doing something similar is really a risk.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
OSZAR »