今天易天科技在开发由一个网站向另外一个网站提交信息的接口时,由于接口只支持中文的UTF-8编码方式,而网站又是GB2312编码,用UTF-8做的页面可以正常发送短信了,但想想这样两种编码分开来,对网站系统的整合还是很方便,易天技术试了很多代码,终于还是调试出这个GB2312转UTF-8编码的函数,分享出来给大家。
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(字符串变量)

