I’m generating HTML dynamically using Delphi strings (Delphi XE). What is the correct way to encode accentuated characters into my HTML?
var
s : string;
myHTML : string;
(...)
s:= 'programação';
myHTML:=
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'+#10+
'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'+#10+
(...)
'<title>OmneeK Server - Intraweb</title>'+#10+
'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'+#10+
(...)
myHTML:= myHTML + '<font color="red">' + s + '</font>';
(...)
with the above code I get (from the browser):
"programa��o"
I’ve tried with HTMLEncode but the result is the same. I’m using ICS components to handle the HTTP requests.
Yes. A Delphi
Stringis aUnicodeStringin XE. Delphi has had native support for UTF-8 encoded strings since D2009.One thing you can do is simply assign the original
UnicodeStringto aUTF8Stringvariable and let the RTL encode the Unicode data to UTF-8 for you, then you can send the raw bytes of theUTF8Stringto the client:Another option is to send the UTF-8 data as a
TStream. You can place aUTF8Stringinto aTMemoryStream:Or place the original
UnicodeStringinto aTStringStreamwithTEncoding.UTF8applied to it: