asp如何转换gb2312字符串为utf-8字符串
发布网友
发布时间:2023-10-15 13:49
我来回答
共4个回答
热心网友
时间:2024-11-26 04:51
写一个程序将gb码转换成UTF8, 以下是我用的函数
使用方法: UTF8STR = GB2UTF("GB码内容", 1)
不过我不能理解,你是怎么接收到gb码参数的? 无论是POST还是GET 如果你的页面写好是utf-8, 那你收到的参数应该都是UTF8的编码,难道你整个WEB的页面编码不统一吗?
Private Function GetHex(ByVal str1 As String, IsANSI As Boolean) As String
Dim b1() As Byte
If IsANSI Then b1 = StrConv(str1, vbFromUnicode) Else b1 = str1
Dim s1 As Long, n1 As Long, l1 As Long
l1 = LBound(b1)
n1 = UBound(b1) - l1 + 1
GetHex = Space(n1 * 2)
For s1 = 1 To n1
Mid(GetHex, s1 + s1 - 1, 2) = Replace(Format(Hex(b1(s1 + l1 - 1)), "@@"), " ", "0")
Next
End Function
Private Function U2UTF8(ByVal a_iNum, ByVal is_With_%)
Dim sResult, sTmpResult, caHex, sUTF8
Dim iTemp, iHexNum, i
iHexNum = a_iNum
If iHexNum = "" Then
Exit Function
End If
caHex = ""
sResult = ""
sTmpResult = ""
If (iHexNum < 128) Then
is_With_% = 0
sTmpResult = sTmpResult & Chr(iHexNum)
ElseIf (iHexNum < 2048) Then
sTmpResult = ChrB(&H80 + (iHexNum And &H3F))
iHexNum = iHexNum \ &H40
sTmpResult = ChrB(&HC0 + (iHexNum And &H1F)) & sTmpResult
ElseIf (iHexNum < 65536) Then
sTmpResult = ChrB(&H80 + (iHexNum And &H3F))
iHexNum = iHexNum \ &H40
sTmpResult = ChrB(&H80 + (iHexNum And &H3F)) & sTmpResult
iHexNum = iHexNum \ &H40
sTmpResult = ChrB(&HE0 + (iHexNum And &HF)) & sTmpResult
End If
If Len(sTmpResult) > 0 And is_With_% = 1 Then
i = 1
caHex = GetHex(sTmpResult, False)
Do While i < Len(caHex)
sResult = sResult & "%" & Mid(caHex, i, 2)
i = i + 2
Loop
Else
sResult = sTmpResult
End If
U2UTF8 = sResult
End Function
Function GB2UTF(ByVal a_sStr, ByVal is_With_%)
Dim sGB, sResult, sTemp
Dim iLen, iUnicode, iTemp, i
sGB = Trim(a_sStr)
iLen = Len(sGB)
For i = 1 To iLen
sTemp = Mid(sGB, i, 1)
iTemp = Asc(sTemp)
If (iTemp > 127 Or iTemp < 0) Then
iUnicode = AscW(sTemp)
If iUnicode < 0 Then
iUnicode = iUnicode + 65536
End If
Else
iUnicode = iTemp
End If
sResult = sResult & U2UTF8(iUnicode, is_With_%)
Next
GB2UTF = sResult
End Function
热心网友
时间:2024-11-26 04:51
有一个折中的办法,就是不管是utf-8还是gb2312
对于url编码来说,都认,所以,建议你将接收到的不确定是utf-8还是gb2312编码的那些字符用url编码: server.urlencode()
热心网友
时间:2024-11-26 04:52
server.urlencode()
热心网友
时间:2024-11-26 04:52
用server.urlencode()