I have a mixed textfile with utf8 and iso8859-1.
Why is it mixed in the first place? A text file should never use more than one charset at a time. Are you sure this is actually a text file, and not a structured binary file?
I hope and wish there is a way to do the whole file to an iso8859-1 file but how can I do this?
You can't convert the whole file in one go. You need to know which portions are UTF-8 and which portions are ISO-8859-1, and then convert each portion separately as needed.
What I wish is something like this:
for a:=1 to 100 do begin
MyString:=MyTextline[a];
If MyString contains UTF8 then begin
convert MyString to iso8859-1
end;
end;
UTF-8 has a very well-defined structure to it, so it is very easy to detect. The first byte of a UTF-8 sequence always has its bits set to either 0xxxxxxx, 110xxxxx, 1110xxxx, or 11110xxx. If 0xxxxxxx, the byte contains the complete codepoint as-is, otherwise the number of 1 bits specifies the number of total bytes in the sequence (2, 3, or 4), and the extra bytes all have their bits set to 10xxxxxx. The combined x bits from the entire sequence forms the actual Unicode codepoint.
ISO-8859-1 is also well-defined.
So, while scanning through your file data, look for these specific bit patterns, and if detected then convert between UTF-8 and ISO-8859-1 as needed.
On the other hand, ISO-8859-1 supports only a very small subset of Unicode, whereas UTF-8 supports the entire Unicode repertoire. So, why would you want to convert from UTF-8 to ISO-8859-1 and risk data loss? If a valid UTF-8 sequence represents a Unicode codepoint that ISO-8859-1 does not support, you will lose that codepoint during the conversion. There is less chance of data loss if you go the other way. Any valid ISO-8859-1 byte can be converted to UTF-8 without data loss. And it is easier to check if a given single byte is NOT in a valid UTF-8 sequence than it is to check if a given range of bytes IS a valid UTF-8 sequence.
Either way, do note that there is some overlap (outside of the ASCII range) between UTF-8 and ISO-8859-1, so it is possible (albeit unlikely) that valid ISO-8859-1 byte sequences would ALSO be valid UTF-8 byte sequences. Without any *context* about what the data is supposed to represent, it may not always be easy to differentiate between them correctly. So be prepared to accept some margin of false positives while converting.