I am trying to replace character (decimal value 197) in a UTF-8 file with character (decimal value 65)
I can load the file and put it in a string (may not need to do that though)
SS := TStringStream.Create(ParamStr1, TEncoding.UTF8);
SS.LoadFromFile(ParamStr1);
//S:= SS.DataString;
//ShowMessage(S);
However, how do i replace all 197’s with a 65, and save it back out as UTF-8?
SS.SaveToFile(ParamStr2);
SS.Free;
————– EDIT —————-
reader:= TStreamReader.Create(ParamStr1, TEncoding.UTF8);
writer:= TStreamWriter.Create(ParamStr2, False, TEncoding.UTF8);
while not Reader.EndOfStream do
begin
S:= reader.ReadLine;
for I:= 1 to Length(S) do
begin
if Ord(S[I]) = 350 then
begin
Delete(S,I,1);
Insert('A',S,I);
end;
end;
writer.Write(S + #13#10);
end;
writer.Free;
reader.Free;
Decimal
197is hexC5, and decimal65is hex41.C5is not a valid UTF-8 octet by itself, but41is. So I have to assume you are actually referring to Unicode codepointsU+00C5 LATIN CAPITAL LETTER A WITH RING ABOVEandU+0041 LATIN CAPITAL LETTER Ainstead.U+00C5is encoded in UTF-8 asC3 85, andU+0041is encoded as41. To do what you are asking, you have to decode the UTF-8, replace the codepoints, then re-encode back to UTF-8.StringReplace()will work just fine for that, eg:Or:
Update: based on other comments, it looks like you are not actually interested in Unicode codepoint
U+00C5 LATIN CAPITAL LETTER A WITH RING ABOVE, but rather inU+015E LATIN CAPITAL LETTER S WITH CEDILLAinstead, which is encoded in UTF-8 asC5 9E. If that is true, then simply replaceÅwithŞwhen callingStringReplace()after the UTF-8 data has been decoded: