vb怎么把中文字符串转成网页传输的中文编码 10
好像有转成的编码两种呀
比如“亮掉”,用gshuli的方法是得到C1%C1%B5%F4 。在百度上搜索这个词,看地址是%E4%BA%AE%E6%8E%89。。请问怎么得到后面的那个编码呀 展开
那我知道是另一种编码了
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
Private Const CP_UTF8 = 65001
'调用Dim b() As Byte,msgbox Utf8ToUnicode(b)
Function Utf8ToUnicode(ByRef Utf() As Byte) As String
Dim lRet As Long
Dim lLength As Long
Dim lBufferSize As Long
lLength = UBound(Utf) - LBound(Utf) + 1
If lLength <= 0 Then Exit Function
lBufferSize = lLength * 2
Utf8ToUnicode = String$(lBufferSize, Chr(0))
lRet = MultiByteToWideChar(CP_UTF8, 0, VarPtr(Utf(0)), lLength, StrPtr(Utf8ToUnicode), lBufferSize)
If lRet <> 0 Then
Utf8ToUnicode = Left(Utf8ToUnicode, lRet)
Else
Utf8ToUnicode = ""
End If
End Function
Private Sub Command1_Click()
MsgBox UTF8EncodeURI("亮掉")
End Sub
我这里有一个方法,看到你写专程!
/ / / <summary>
?/ / /首选编码的代码页名称
??/ / / </摘要>
??/ / /的<param Name="srcName">原始编码格式</ PARAM>
??/ / / <param name="convToName">转换编码格式</参数>
??/ / / <param Name="value">的需要转换的字符串</ PARAM>
???/ / /该<returns>返回转换后的字符串</>
??公共静态字符串EnCodeCovert(srcName字符串,convToName字符串,字符串值)
??
????System.Text.Encoding srcEncode = System.Text.Encoding.GetEncoding(srcName)的;
????System.Text.Encoding convToEncode = System.Text.Encoding.GetEncoding(convToName)的;
????字节[]字节= srcEncode.GetBytes(值);
????System.Text.Encoding.Convert(srcEncode,convToEncode,字节,0,bytes.Length);
???返回convToEncode.GetString(字节);
??}
你在过去的尝试。
我还没有测试,但编译器是没有问题的,你可以试试。
我没有测试,没有你说的环境! ! !
Private Sub Command1_Click()
MsgBox UnCode("%CA%E4%B5%C4%D6%D0%CE%C4%B1%E0")
End Sub
'编码
Function EnCode(DescString As String) As String
Dim Buff() As Byte
Dim Tmp() As String
Dim i As Long, n As Long
Buff = StrConv(DescString, vbFromUnicode)
n = UBound(Buff)
ReDim Tmp(n)
For i = 0 To n
Tmp(i) = (Hex(Buff(i)))
Next i
EnCode = "%" & Join(Tmp, "%")
End Function
'反编码
Function UnCode(Code As String) As String
Dim TmpCode As String
Dim Tmp() As String, TmpBuff() As Byte
Dim i As Long, n As Long
TmpCode = Right(Code, Len(Code) - 1)
Tmp = Split(TmpCode, "%")
n = UBound(Tmp)
ReDim TmpBuff(n)
For i = 0 To n
TmpBuff(i) = Val("&H" & Tmp(i))
Next i
UnCode = StrConv(TmpBuff, vbUnicode)
End Function
'这可是我的原创,把分给我吧
这是编码规则不同,这个你百度一下吧
好像有转成的编码两种呀
比如“亮掉”,用您的方法是得到C1%C1%B5%F4 。在百度上搜索这个词,看地址是%E4%BA%AE%E6%8E%89。。请问怎么得到后面的那个编码呀
Dim x() As Byte
aa = "输的中文编"
x = StrConv(aa, vbFromUnicode)
For i = 0 To UBound(x)
Print "%"; Hex$(x(i));
Next
End Sub
好像有转成的编码两种呀
比如“亮掉”,用您的方法是得到C1%C1%B5%F4 。在百度上搜索这个词,看地址是%E4%BA%AE%E6%8E%89。。请问怎么得到后面的那个编码呀
这是转成utf-8编码
Function UTF8EncodeURI(szInput) '转成utf-8编码函数
Dim wch, uch, szRet
Dim x
Dim nAsc, nAsc2, nAsc3
If szInput = "" Then
UTF8EncodeURI = 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
UTF8EncodeURI = szRet
End Function
Private Sub Command1_Click()
aa = "亮掉"
Print UTF8EncodeURI(aa)’调用转成utf-8编码函数
End Sub
?strconv(chrw(&HE4CA),vbUnicode)