今天易天科技在開發(fā)由一個網(wǎng)站向另外一個網(wǎng)站提交信息的接口時,由于接口只支持中文的UTF-8編碼方式,而網(wǎng)站又是GB2312編碼,用UTF-8做的頁面可以正常發(fā)送短信了,但想想這樣兩種編碼分開來,對網(wǎng)站系統(tǒng)的整合還是很方便,易天技術(shù)試了很多代碼,終于還是調(diào)試出這個GB2312轉(zhuǎn)UTF-8編碼的函數(shù),分享出來給大家。
ASP/Visual Basic代碼
- Private Function YiskyGBtoUTF8(szInput)
- Dim wch, uch, szRet
- Dim x
- Dim nAsc, nAsc2, nAsc3
- If szInput = "" Then
- YiskyGBtoUTF8= szInput
- Exit Function
- End If
- For x = 1 To Len(szInput)
- wch = Mid(szInput, x, 1)
- nAsc = AscW(wch)
- If nAsc < 0 Then nAsc = nAsc + 65536
- If (nAsc And &HFF80) = 0 Then
- szRet = szRet & wch
- Else
- If (nAsc And &HF000) = 0 Then
- uch = "%" & Hex(((nAsc \ 2 ^ 6)) Or &HC0) & Hex(nAsc And &H3F Or &H80)
- szRet = szRet & uch
- Else
- uch = "%" & Hex((nAsc \ 2 ^ 12) Or &HE0) & "%" & _
- Hex((nAsc \ 2 ^ 6) And &H3F Or &H80) & "%" & _
- Hex(nAsc And &H3F Or &H80)
- szRet = szRet & uch
- End If
- End If
- Next
- YiskyGBtoUTF8= szRet
- End Function
使用時:a=GBtoUTF8(字符串變量)