扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
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
字符编码转换吗?
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
'这里可以类推出好几种。
这个就是问的int和char类型的存储与位操作。
int类型32位,char类型8位。
代码如下
#include stdio.h
int main()
{
int a = -368;
char b[4];
int i;
for(i = 0; i 4; i++)
{
b[i] = a 0xff;
a = a 8;
}
for(i = 0; i 4; i++)
printf("b[i] = %d\n", (int)b[i]);
i = (b[0]0xff) | (b[1]0xff)8 | (b[2]0xff)16 | (b[3]0xff)24;
printf("i = %d\n", i);
return 0;
}
Dim b(1) As Byte, x As Integer
x = -1234
b(0) = x And HFF
b(1) = x / H100 And HFF
字节数组b()储存的就是负数-1234
在VB.NET中如果需要将一个字符串转换成字节数组,可以使用System.Text命名空间中某个编码对象的GetBytes方法。
Dim strText As String = "This is an original string"
VB6.0可使用StrConv 函数将字符串转换为字节流(数组)。
StrConv 函数,返回按指定类型转换的 Variant (String)。
实例代码:
Option Explicit
Dim strSj As String
Dim aryContent() As Byte
Dim i As Long
Private Sub Command1_Click()
strSj = Text1
aryContent = StrConv(strSj, vbFromUnicode)
For i = 0 To UBound(aryContent)
Print Hex(aryContent(i))
Next
End Sub
Private Sub Form_Load()
Text1 = "vb中怎么将字符类型转换为字节类型"
End Sub
vbFromUnicode,将字符串由 Unicode 转成系统的缺省码页。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流