How to convert utf-8 text to ANSI. Delphi 2006

Asked

Viewed 2,525 times

5

How to convert text utf-8 for ANSI. The words they use "~" and "´" are coming out wrong, see for example the use of the word "PREPARATION" coming out: "PREPARES 플O" when generating file .CSV using Delphi 2006.

  • It will depend on the type of object you are using to save this file. Post the snippet of code that makes our life easier.

  • This is the part where I pass on the data to be recorded.. if Type = 'C' then Write(ARQTXT, Ereplicate(Ansitoutf8(Text), ' ', Size)); Write(ARQTXT, ';'); Application.Processmessages

  • Solved! Just do so: // Before you start writing to the Write(ARQTXT,#$EF+#$BB+#$BF); //Then use utf8encode() when saving the data to the... Write(ARQTXT,utf8encode(Text)); .

  • @Joaoboscodosreisbecker If I could help with my answer, you can accept the answer by clicking on the left side of it. If you need any more help, let us know.

1 answer

0

Try using the following code, according to the answer given to this question in Stackoverflow the problem was solved using the internal function of the Delphi UTF8toAnsi and one more function I posted below, using the version Delphi 2010.

function TFormMain.convertir_utf8_ansi(const Source: string):string;
var
   Iterator, SourceLength, FChar, NChar: Integer;
begin
   Result := '';
   Iterator := 0;
   SourceLength := Length(Source);
   while Iterator < SourceLength do
   begin
      Inc(Iterator);
      FChar := Ord(Source[Iterator]);
      if FChar >= $80 then
      begin
         Inc(Iterator);
         if Iterator > SourceLength then break;
         FChar := FChar and $3F;
         if (FChar and $20) <> 0 then
         begin
            FChar := FChar and $1F;
            NChar := Ord(Source[Iterator]);
            if (NChar and $C0) <> $80 then break;
            FChar := (FChar shl 6) or (NChar and $3F);
            Inc(Iterator);
            if Iterator > SourceLength then break;
         end;
         NChar := Ord(Source[Iterator]);
         if (NChar and $C0) <> $80 then break;
         Result := Result + WideChar((FChar shl 6) or (NChar and $3F));
      end
      else
         Result := Result + WideChar(FChar);
   end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var source, result: string;
begin
  source := 'PREPARAÇÃO';
  result := Utf8toAnsi(convertir_utf8_ansi(source));
end;
  • 1

    While this link may answer the question, it is best to include the essential parts of the answer here and provide the link for reference. Replies per link only can be invalidated if the page with the link is changed. - Of Revision

  • 1

    @Uzumakiartanis I tried to further detail the information, I hope I was able to improve the answer.

  • this code posted here, is not what is in the question in the S.O. in English as the code that doesn’t work????

Browser other questions tagged

You are not signed in. Login or sign up in order to post.