Convert string to unicode utf-8 in ColdFusion

I need to convert a string to UTF8 encoded format, and I'm not sure how to do this.

Is there any function in ColdFusion to convert a string to UTF-8, for example, on this website ?

For example, typing in "stackoverflow.com/questions/ask" on the above website gives the result:

\ x73 \ x74 \ x61 \ x63 \ X6b \ x6F \ x76 \ x65 \ x72 \ x66 \ x6C \ x6F \ x77 \ x2E \ x63 \ x6F \ x6D \ x2F \ x71 \ x75 \ x65 \ x73 \ x74 \ x69 \ x6F \ x6E \ x73 \ x2F \ x61 \ x73 \ X6b

I am not very good at coding, however my instructions were to encode a string in UTF-8. The example I gave gave, for example, the encoded result below.

/ re / g / 434 / t // 4r3 / t434 / 4t / T3 / 3 / 4t / 43tt / 53 /

I'm not sure if this is a real representation of the encoded string or if it was just printed to give a visual example. Is there a format that looks like this? And is it different from the format from the first example?

Thank!

+4
source share
2 answers

I think you can use a combination of CharsetDecode () and CharsetEncode () to accomplish this.

<cfset my_string = "test">
<cfset binary_my_string = CharsetDecode(my_string, "ASCII")>
<cfset utf8_my_string = CharsetEncode(binary_my_string, "utf-8")>

You just need to replace the correct initial encoding for "ASCII" in my example.

+5
source
<cfset str = "stackoverflow.com/questions/ask">
<cfset hexStr = "">

<cfloop index="i" from="0" to="#len(str)-1#">
    <!--- Pick out each character in the string. Remember that charAt() starts at index 0. --->
    <cfset ch = str.charAt(i)>
    <!--- The decimal value of the Unicode character. ColdFusion uses the Java UCS-2 representation of Unicode characters, up to a value of 65536.  --->
    <cfset charDecVal = asc(ch)>
    <!--- The decimal value of the character, upper-casing the letters.--->
    <cfset charHexVal = uCase(formatBaseN(charDecVal,"16"))>
    <!--- Append the characters together into a Hex string, using delimiter '\x' --->
    <cfset hexStr = hexStr & "\x" & charHexVal>
</cfloop>
<cfscript>
    writeoutput(hexStr);
</cfscript>
0
source

Source: https://habr.com/ru/post/1657099/


All Articles