重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Private Function Conver_Hex(ByVal L As Long) As Array
创新互联服务项目包括榕城网站建设、榕城网站制作、榕城网页制作以及榕城网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,榕城网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到榕城省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
Dim str_L As String = Hex(L)
If str_L.Length \ 2 0 Then
str_L = "0" str_L
End If
Dim byt(str_L.Length / 2 - 1) As Byte
For i = 0 To str_L.Length / 2 - 1
byt(i) = Convert.ToByte(str_L.Substring(2 * i, 2), 16)
Next
Return byt
End Function
'下面是调用代码
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim L As Double = 123456
Dim byt() As Byte = Conver_Hex(L)
End Sub
在VB.NET中如果需要将一个字符串转换成字节数组,可以使用System.Text命名空间中某个编码对象的GetBytes方法。
Dim strText As String = "This is an original string"
ToCharArray()
例如:str="asdfafafafaf"
str.toCharArray就是一个Char()数组了。
你搞错了一点:Unicode字符规定,每个字符占用两个字节,包括英文和数字,所以你只需将:
MarshalAs(UnmanagedType.ByValTStr, sizeconst:=3) Public s As String
改成:
MarshalAs(UnmanagedType.ByValTStr, sizeconst:=6) Public s As String
即可!
numHex =Integer.Parse(strHex, Globalization.NumberStyles.AllowHexSpecifier)
或
numHex = Integer. Parse("H" strHex)
字符编码转换吗?
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
'这里可以类推出好几种。