2016-10-07 29 views
4

我需要將字符串轉換爲UTF8編碼格式,我不知道如何繼續。在ColdFusion中將字符串轉換爲utf-8 unicode

有ColdFusion的中的任何函數將字符串轉換爲UTF-8,如本website

例如,鍵入在「stackoverflow.com/questions/ask」到上述網站給出結果:

\ X73 \ X74 \ X61 \ X63 \ X6B \ x6F \ X76 \ X65 \ x72 \ x66 \ x6C \ x63 \ x6F \ x6D \ x2F \ x71 \ x75 \ x65 \ x73 \ x74 \ x69 \ x6F \ x6E \ x73 \ x2F \ x61 \ x73 \ x6B

我對編碼不是很熟悉,但是我的指令是將一個字符串編碼爲UTF-8。舉例來說,我給出了例如下面的編碼結果。

/RE/R/434 /噸// 4R3/T434/4步/ T3/3/4步/ 43tt/53/

我不知道這是否是一個真實的表示編碼的字符串,或者只是輸入了一個可視化的例子。有沒有看起來像這樣的格式?它與第一個例子的格式不同嗎?

謝謝!

+2

(編輯)* RE:我給出的例子給出了下面例子的編碼結果*您能給我們一些上下文嗎?這個例子的來源是什麼?如果這是一個API,你能發佈URL嗎? * RE:...例如在本網站上*該網站選擇以十六進制格式顯示utf-8二進制文件。 [TryCF Example](http://trycf.com/gist/dced3d873ca415f2a568f81f7ba99ed2/acf2016?theme=monokai)。注意結果是一樣的,只是沒有前導的「\ x」:即'46 6F 6F 20 ...'而不是'\ x46 \ x6F \ x6F \ x20 ....'。 – Leigh

+0

這是Tipalti文檔系統。你可以在這裏看到自己的文檔:https://support.tipalti.com/Content/Topics/Development/iFrames/iframe_authentication.htm 我試圖獲得認證工作! – MerrickC

+2

感謝您的額外信息。 「編碼爲UTF8 ...」部分只是[使用下面提到的charsetDecode](http://stackoverflow.com/a/39926218/104223)。 C#代碼簡單地抓取字符串的utf8二進制文件,即'charsetDecode(someString,「utf-8」)'。如果您在完整的身份驗證例程中遇到問題,則應該使用您嘗試過的代碼和結果打開一個單獨的線程。 – Leigh

回答

3

我認爲你可以使用的CharsetDecode()CharsetEncode()的組合來完成這個任務。

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

你只需要在我的例子來代替the correct initial encoding「ASCII」

+1

這似乎只是將字符串轉換回初始字符串 – MerrickC

+2

因爲ASCII是UTF8的子集。看來你真正的問題是更大環境的一部分。就像我上面提到的那樣,最好用完整的代碼和結果來打開一個新的線程,以便有人可以幫助您實現真正的目標,即實現api的身份驗證規則。 – Leigh

1
<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>