Converting Text From UTF-8 to ISO-8859-1
Posted June 5, 2010 by Bobby Hartsfield
Recently, I had a need to convert text from UTF-8 to iso-8859-1. Actually, the need was to come up with a more efficient way of doing so. I'll spare you the why, when and where and get right to the how.
After digging into the java.nio.charset.Charset, I came up with the following function. (commented for your viewing pleasure)
<cfscript>
function convertutfToIso(str)
{
//var a struct to store all of our stuff in
var local = {};
//Create an object of java.nio.charset.Charset
local.charSetObj = createobject("java", "java.nio.charset.Charset");
//What the text is currently
local.convertFrom = local.charsetObj.forName('iso-8859-1');
//What to convert the text to
local.convertTo = local.charsetObj.forName('utf-8');
//return the converted text
return local.convertTo.decode(local.convertFrom.encode(str)).ToString();
}
</cfscript>
Now anytime that I need to ensure some provided text is iso-8859-1, I can simply convert it like so:
<cfset isoText = convertUtfToIso(utfText) />
This worked well on some rather large chunks of text with many characters that needed to be converted.
Of course you can change the "to" and "from" to suit your needs but that should get you started.
Enjoy.