Converting Text From UTF-8 to ISO-8859-1

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)

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();

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.


Most Recent Photos