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

1

How to convert text utf-8 to ANSI . The words that use " ~ " and " '" are going out wrong, for example use the word " PREPARATION " PREPARE " when generating .CSV file using Delphi 2006 .

    
asked by anonymous 15.02.2017 / 19:17

1 answer

0

Try using the following code, based on the answer given to this question in StackOverflow the problem was solved using the built-in Delphi function UTF8toAnsi and one more function I posted below, using the Delphi 2010 / p>

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;
    
29.08.2017 / 15:53