请问VB中如何将Byte数组转换成String型?楼主yjg5(如风逐月)2005-03-04 14:35:21 在 VB / 基础类 提问
请问VB中如何将Byte数组转换成String型?
问题点数:20、回复次数:8
Top
1 楼myhgyp(也许是这样的,信不信由你)回复于 2005-03-04 14:40:01 得分 3Public Sub StringToByteArray(ByteArray() As Byte, str As String, size As Long) 'String转换成Byte数组
Dim i As Long, c As String, icode As Integer
For i = 0 To size - 1
ByteArray(i) = AscB(MidB(str, i + 1, 1))
Next i
End Sub
Public Sub ByteArrayToString(str As String, hex() As Byte, size As Long) 'Byte数组转换成String
Dim i As Long, c As Byte
str = ""
For i = 0 To size - 1
str = str & ChrB(hex(i))
Next i
End Sub
Top
2 楼viena(维也纳N02)回复于 2005-03-04 14:43:50 得分 3如果Byte数组中存的是Unicode,直接赋值;
如果不是Unicode,用Strconv转换一下
Top
3 楼myhgyp(也许是这样的,信不信由你)回复于 2005-03-04 14:46:16 得分 3或:
Public Function StringToByteArray(str As String, size As Long) as byte() 'String转换成Byte数组
Dim i As Long, c As String, icode As Integer
Dim ByteArray() as Byte
Redim ByteArray(size)
For i = 0 To size - 1
ByteArray(i) = AscB(MidB(str, i + 1, 1))
Next i
StringToByteArray=ByteArray
End Sub
Public Function ByteArrayToString(hex() As Byte, size As Long) As String, 'Byte数组转换成String
Dim i As Long, c As Byte
str = ""
For i = 0 To size - 1
str = str & ChrB(hex(i))
Next i
ByteArrayToString=str
End Sub
Top
4 楼sinos_sinos(挑战不是障碍 人生处处精彩)回复于 2005-03-04 14:52:54 得分 3Dim byteAry(10) as Byte
Dim Str5 as String
byteAry(0) = 25
byteAry(1) = 144
byteAry(2) = 97
byteAry(3) = 98
byteAry(4) = 99
Str5 = StrConv(byteAry, vbUniCode)
Top
5 楼viena(维也纳N02)回复于 2005-03-04 14:54:13 得分 3楼上,Byte数组和String可以直接相互转换的
'VB中的String是Unicode
Dim str1 As String
Dim str2 As String
Dim abyte1() As Byte
Dim abyte2() As Byte
str1 = "字符串abcd1234"
abyte1 = str1
abyte2 = StrConv(str1, vbFromUnicode)
str2 = abyte1
Debug.Print "Unicode的byte数组直接赋值:" & str2
str2 = abyte2
Debug.Print "没转转换为Unicode是这样:" & str2
str2 = StrConv(abyte2, vbUnicode)
Debug.Print "转换为Unicode就可以了:" & str2
Top
6 楼homezj(小吉)回复于 2005-03-04 14:56:16 得分 3viena说得对!
存放文本的Byte数组可以与String直接转换。
dim a() as byte,s as string
---------------------------
在内存中的相互转换
s="hggjh"
a=s '直接转为Unicode编码的数组
s=a '将Unicode编码的数组转为String
----------------------------------
-------------------------------------------
对于存在文件中的文本,读入byte数组为ANSI编码
s=strconv(a,vbunicode) '可将文件读入byte数组转为String
a=strconv(s,vbfromunicode) '可将String转为ANSI编码存入byte数组
------------------------------------------------------------
常用的转换就这些了