String encoding conversion

20 July 2007 10:42 (EST)
You can use following code to convert string encoding using Java library:

<cfset oCharset = createObject("java","java.nio.charset.Charset") />
<cfset fromCharSet = oCharset.forName('utf-8') />
<cfset toCharSet = oCharset.forName('iso-8859-1') />
<cfset b = toCharSet.decode(fromCharSet.encode(q)).ToString() />

Every implementation of the Java platform is required to support the following standard charsets. Consult Java release documentation for your implementation to see if any other charsets are supported. Following charsets are supported with Java 2 Platform SE v1.4.2:

US-ASCII = Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set
ISO-8859-1 = ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
UTF-8 = Eight-bit UCS Transformation Format
UTF-16BE = Sixteen-bit UCS Transformation Format, big-endian byte order
UTF-16LE = Sixteen-bit UCS Transformation Format, little-endian byte order
UTF-16 = Sixteen-bit UCS Transformation Format, byte order

Tags string encoding java cfobject charset

AddThis Social Bookmark Button

Google Search API and correct encoding ColdFusion 8 Launch Party in Melbourne

Comments

PaulH: 20 July 2007 14:14 (EST)

java supports rather a lot more encodings than those & i've yet to see an install that didn't support the "optional" charsets. see:
http://www.sustainablegis.com/projects/i18n/charsetTB.cfm
your example of converting utf-8 to latin-1 doesn't make a whole lot of sense as the majority of "chars" in unicode *can't* be converted to latin-1 at least that make any sense. the other way round works better.

Dmitry Yakhnov: 20 July 2007 17:07 (EST)

That example fix the issue with Google Search API request through cfinvoke I have blogged about yesterday.

Drop a comment... don't be shy