function MakeSafeHTMLText2(TheText: widestring): string;
var
Idx: Integer; // loops thru characters of TheText
Ch: wideChar; // each charactor in TheText
begin
Result := '';
for Idx := 1 to Length(TheText) do
begin
Ch := TheText[Idx];
case Ch of
'<': Result := Result + '<';
'>': Result := Result + '>';
'&': Result := Result + '&';
'"': Result := Result + '"';
else
begin
if (Ch < #32) or (Ch >= #127) then // Result := Result + '&#' + IntToStr(Ord(Ch)) + ';'
Result := Result + '&#x' + InttoHex(Integer(ch),2) + ';'
else
Result := Result + Ch;
end;
end;
end;
end;