扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
字符编码转换吗?
创新互联专业为企业提供右江网站建设、右江做网站、右江网站设计、右江网站制作等企业网站建设、网页设计与制作、右江企业网站模板建站服务,10多年右江做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
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
'这里可以类推出好几种。
(1)在Form1上布置一个Label,用来显示键盘码
(2)窗体代码如下
Imports System.Windows.Forms
Imports System.IO
Imports System.Net
Imports System.Windows.Forms
Imports System.IO
Imports System.Net
Public Class Form1
Private Sub Form1_Load(sender As Object,
e As EventArgs) Handles MyBase.Load
KeyPreview = True
End Sub
Private Sub Form1_KeyDown(sender As Object,
e As KeyEventArgs) Handles MyBase.KeyDown
Label1.Text = e.KeyValue
End Sub
End Class
按键盘上的键,Label1将显示对应键的键盘码
GB2312是GBK编码的子集,使用GBK编码函数即可
'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
原来还有这么个功能,那有没有转换为拼音的方法?
我比你运气好,两个都有效:
Public Class Form1
Private Declare Function LCMapString Lib "kernel32" Alias "LCMapStringA" (ByVal Locale As Integer, ByVal dwMapFlags As Integer, ByVal lpSrcStr As String, ByVal cchSrc As Integer, ByVal lpDestStr As String, ByVal cchDest As Integer) As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cString As String = "中华人民共和国"
cString = (StrConv(cString, VbStrConv.TraditionalChinese))
MsgBox(cString)
cString = (StrConv(cString, VbStrConv.SimplifiedChinese))
MsgBox(cString)
Dim rString As String = Space(14)
LCMapString(H804, H4000000, cString, 14, rString, 14)
MsgBox(rString)
cString = rString
LCMapString(H804, H2000000, cString, 14, rString, 14)
MsgBox(rString)
End Sub
End Class
StrConv参数不对,api 函数Long型要改为Integer类型。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流