ASP.NET 汉字转化为GB2312时出问题了
在网站上搜索时“兰芝睡眠面膜20ML”在转换为GB2312时结果为:/x5170/x829D/x7761/x7720/x9762/x819C20ML 在将他转换为:汉字时报错:字符串的末尾有其他无法分析的字符 应该是20ml这个没转换对,我用“兰芝睡眠面膜”这个查询就没问题,大家帮帮忙啊。我两个函数为:
汉字转换为GB2312:
Public Function Escape(ByVal src As String) As String
If src Is Nothing Then
Return Nothing
End If
Dim i As Integer
Dim j As Char
Dim tmp As StringBuilder = New StringBuilder
For i = 0 To src.Length - 1
j = src.Chars(i)
If Char.IsDigit(j) Or Char.IsLower(j) Or Char.IsUpper(j) Then
tmp.Append(j)
ElseIf (AscW(j) < 16 * 16) Then
tmp.Append("%")
If AscW(j) < 16 Then
tmp.Append("0")
End If
tmp.Append(Hex(AscW(j)))
Else
tmp.Append("%u")
If (AscW(j) < 16 * 16 * 16) Then
tmp.Append("0")
End If
tmp.Append(Hex(AscW(j)))
End If
Next
Return tmp.ToString()
End Function
GB2312转换为汉字:
Public Function EscToStr(ByVal c As String) As String
Dim tmp, str1, str2, str3, str4 As String
Dim i, outend1, outend2 As Integer
i = 1
outend1 = 1
Do While outend1 <> 0
str1 = c
outend1 = InStr(i, str1, "/x")
i = outend1 + 1
outend2 = InStr(i, str1, "/x")
If outend2 = 0 Then
outend2 = Len(str1) + 1
End If
str2 = Mid(str1, outend1 + 2, outend2 - outend1 - 2)
str3 = "&H" & str2
str4 = ChrW(str3)
tmp = tmp & str4
If outend2 = (Len(str1) + 1) Then
outend1 = 0
End If
Loop
EscToStr = tmp
End Function
--------------------编程问答-------------------- 自己顶下 --------------------编程问答-------------------- 直接
String a = Microsoft.JScript.GlobalObject.escape("兰芝睡眠面膜20ML");
即可
注意添加jscript.dll引用 --------------------编程问答-------------------- 我用VB.NET哦,好像没有这个吧String a = Microsoft.JScript.GlobalObject.escape("兰芝睡眠面膜20ML");
不懂 --------------------编程问答--------------------
补充:.NET技术 , ASP.NET