Lenguaje de programación Borland Delphi
A continuación mostramos una función a la que se le pasa como parámetro un texto y lo devuelve convertido en UTF-8 con BOM (Byte Order Mark o Marca de orden de bytes).
function convertirTextoUTF8conBOM (texto : string) : String;
const
cUTF8 = $BFBBEF; //hexadecimal para BOM
var
textoUTF8 : UTF8String;
valorBOM : Integer;
textoTmp : TStringStream;
begin
if (Trim(texto) <> '') then
begin
textoUTF8 := UTF8Encode (texto);
valorBOM := cUTF8;
textoTmp := TStringStream.Create('');
//incluimos BOM
textoTmp.WriteBuffer(valorBOM, sizeof(valorBOM) - 1);
//añadimos resto del texto
textoTmp.WriteBuffer(textoUTF8[1], Length(textoUTF8) * Sizeof(textoUTF8[1]));
Result := textoTmp.DataString;
end
else
Result := '';
end;
Un ejemplo de uso:
var
cadena : string;
begin
cadena := '<?xml version="1.0" encoding="utf-8"?><factura></factura>';
cadena := convertirTextoUTF8conBOM (cadena);
MessageDlg('Texto convertido a UTF-8 con BOM: ' + cadena,
mtInformation, [mbok], 0);
end;
Publicado el: 2014-05-15