0

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

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

4

1 回答 1

2
<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 固定为 2 个字节,因此它不能支持所有 unicode 字符(因为每个字符最多可以有 4 个字节)。但是你到底想在这里实现什么?

注意:如果您运行此示例并返回超过 3 个字节,请确保 CF 将文件选择为 UTF-8(带有 BOM)。

于 2018-11-27T21:01:57.827 回答