Detecting Character Encoding In Coldfusion

Most of the time I only work with UTF-8 or UTF-16 character encodings. With that said, recently I was working on an old coldfusion project. Do to database restrictions, required all inputs from the user into the application to be Latin-1 (ISO-8859-1) character encoded.

Coldfusion does not have a good way of checking the character encoding of a string, so I had to pull out some of my Java skillz.

Dropping into Java from ColdFusion is a lot easier then it sounds. Check it out

<cfscript>   
   //use this to test character encoding. We only want Latin-1
    encoder = createObject("java", "java.nio.charset.Charset").forName("ISO-8859-1").newEncoder();
 
    if(encoder.canEncode(testVar))
    {
        //its Latin-1
    }
    else
    {
        /NOT Latin-1
    }
</cfscript>

The Java layer of Coldfusion is very powerful and under utilized by many developers.

If you want to see some true ColdFusion magic, check out the ColdBox Framework, http://ColdBoxFramework.com

2 Comments

  1. Josh, I have had a persistent problem with ColdFusion messing up my character encoding. I use a lot of Portuguese accents and I encode my pages in UTF-8. I also use cfprocessingdirective pageencoding=”utf-8″ in all of my view files and partials. Unfortunately, I have a problem that affects certain controllers and view files. I’m not unsure why the issue affects only some, and not all… but what happens is characters like “ã” turn into “㣔 … I’m wondering if you have come across something similar and you have any advice…

Leave a Comment.