重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Imports System.Text
创新互联公司主营汉川网站建设的网络公司,主营网站建设方案,成都app软件开发,汉川h5小程序设计搭建,汉川网站营销推广欢迎汉川等地区企业咨询
Public Function StringAsUtf8Bytes(ByVal strData As String) As Byte()
Dim bytes() As Byte
bytes = Encoding.UTF8.GetBytes(strData)
Return bytes
End Function
说明:strData参数是GB2312字符串,函数返回UTF8字节数组
Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As Long) As Long
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
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)
End If
End Function
Private Sub Form_Load()
Dim a As String
Dim b() As Byte
Dim i As Integer
a = "E9 A2 91 E9 81 93 31 00"
ReDim b(UBound(Split(a, " ")) - 1) As Byte
For i = 0 To UBound(Split(a, " ")) - 1
b(i) = "H" Split(a, " ")(i)
Next i
Text1.Text = Utf8ToUnicode(b())
End Sub
字符编码转换吗?
1.字符与gb2312(gbk的子集):
Public Function GBKEncode(ByVal sInput As String) As String
Dim ret_GBKEncode As String = ""
Dim i As Integer
Dim startIndex As Integer = 0
Dim endIndex As Integer
Dim x() As Byte = System.Text.Encoding.Default.GetBytes(sInput) '字符以及字符串在vb2008中都是以unicode编码存储的
endIndex = x.Length - 1
For i = startIndex To endIndex
ret_GBKEncode = "%" Hex(x(i))
Next
Return ret_GBKEncode
End Function
'GBK解码
Public Function GBKDecode(ByVal sInput As String) As String
sInput = sInput.Replace("%", "")
Dim ret_GBKDecode As String = ""
Dim sLen As Integer = sInput.Length
Dim n As Integer = sLen \ 2
Dim sBytes(0 To n - 1) As Byte
'转化为字节码
For i As Integer = 1 To n
sBytes(i - 1) = CByte("H" sInput.Substring(2 * i - 2, 2))
Next
'将字节码转化为字符串
ret_GBKDecode = System.Text.Encoding.Default.GetString(sBytes)
Return ret_GBKDecode
End Function
2.Unicode字符串为UTF-8
Imports System.Text
Public Function StringAsUtf8Bytes(ByVal strData As String) As Byte()
Dim bytes() As Byte
bytes = Encoding.UTF8.GetBytes(strData)
Return bytes
End Function
'这里可以类推出好几种。
Public Function GBKDecode(ByVal sInput As String) As String
sInput = sInput.Replace("%", "")
sInput = sInput.Replace(":", "")
Dim ret_GBKDecode As String = ""
Dim sLen As Integer = sInput.Length
Dim n As Integer = sLen \ 2
Dim sBytes(0 To n - 1) As Byte
'转化为字节码
For i As Integer = 1 To n
sBytes(i - 1) = CByte("H" sInput.Substring(2 * i - 2, 2))
Next
'将字节码转化为字符串
ret_GBKDecode = System.Text.Encoding.UTF8.GetString(sBytes)
Return ret_GBKDecode
End Function
'上面是函数,调用方法
MsgBox(GBKDecode(":%E9%80%81%E8%BE%BE%E6%97%A5%E6%9C%9F%20%E9%80%81%E8%BE%BE%E6%97%B6%E9%97%B4"))
vb内使用unicode,在转换字符串只需要把字符串赋值给字节数组就可以了.但vb6函数不能指定编码形式.不是unicode编码,就是ASCII编码,使用StrConv函数带VbStrConv.Narrow参数返回的字符串转到字节后就是ASCII编码的.要使用其他编码的使用API.VB.net可以使用system.text.encoding.UTF-8.GetBytes()方法获取十六进制编码
根本都不是你说的简单的UTF8编码问题,人家是加密计算,下面的是UTF8编码转化函数
Private Function convert_UTF8(ByVal str As String) As String
Dim byt() As Byte = System.Text.Encoding.GetEncoding("Gb2312").GetBytes(str)
Return System.Text.Encoding.UTF8.GetString(byt)
End Function