James A Mohler
James A Mohler

Reputation: 11120

UTF-8 value of a character in ColdFusion?

In ColdFusion I can determine the ASCII value of character by using asc()

How do I determine the UTF-8 value of a character?

Upvotes: 0

Views: 930

Answers (1)

Alex
Alex

Reputation: 7833

<cfscript>

    x = "漢"; // 3 bytes

    // bytes of unicode character, a.k.a. String.getBytes("UTF-8")
    bytes = charsetDecode(x, "UTF-8");
    writeDump(bytes); // -26-68-94

    // convert the 3 bytes to Hex
    hex = binaryEncode(bytes, "HEX");
    writeDump(hex); // E6BCA2

    // convert the Hex to Dec
    dec = inputBaseN(hex, 16);
    writeDump(dec); // 15121570

    // asc() uses the UCS-2 representation: 漢 = Hex 6F22 = Dec 28450
    asc = asc(x);
    writeDump(asc); // 28450

</cfscript>

USC-2 is fixed to 2 bytes, so it cannot support all unicode characters (as there can be as much as 4 bytes per character). But what are you actually trying to achieve here?

Note: If you run this example and get more than 3 bytes returned, make sure CF picks up the file as UTF-8 (with BOM).

Upvotes: 2

Related Questions