ColdFusion 中字符的 UTF-8 值?
UTF-8 value of a character in ColdFusion?
在 ColdFusion 中,我可以使用 asc()
确定字符的 ASCII 值
如何确定字符的 UTF-8 值?
<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)。
在 ColdFusion 中,我可以使用 asc()
如何确定字符的 UTF-8 值?
<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)。