title: Converting UTF8 to ANSI (ISO-8859-1) in Delphi
body: <p>I have a question about a code that i have to convert UTF8 strings to ANSI strings. My code works for accents in vowels, but with letter Ñ it doesn't work. The code breaks the string. How can I fix this mistake?</p>
  
  <p>The string I have in UTF8: EDIFICIO PEÃ‘AS BLANCAS <br>
  The string I would have in ANSI if correct: EDIFICIO PEÑAS BLANCAS <br>
  The string I have in ANSI now: EDIFICIO PE <br></p>
  
  <p>The code is here:</p>
  
  <pre><code>    function TFormMain.convertir_utf8_ansi(const Source: string):string;
      var
         Iterator, SourceLength, FChar, NChar: Integer;
      begin
         Result := '';
         Iterator := 0;
         SourceLength := Length(Source);
         while Iterator &lt; SourceLength do
         begin
            Inc(Iterator);
            FChar := Ord(Source[Iterator]);
            if FChar &gt;= $80 then
            begin
               Inc(Iterator);
               if Iterator &gt; SourceLength then break;
               FChar := FChar and $3F;
               if (FChar and $20) &lt;&gt; 0 then
               begin
                  FChar := FChar and $1F;
                  NChar := Ord(Source[Iterator]);
                  if (NChar and $C0) &lt;&gt; $80 then break;
                  FChar := (FChar shl 6) or (NChar and $3F);
                  Inc(Iterator);
                  if Iterator &gt; SourceLength then break;
               end;
               NChar := Ord(Source[Iterator]);
               if (NChar and $C0) &lt;&gt; $80 then break;
               Result := Result + WideChar((FChar shl 6) or (NChar and $3F));
            end
            else
               Result := Result + WideChar(FChar);
         end;
      end;
  </code></pre>
  
  <p>Thanks.</p>