VB 如何将一个二进制数组中的数据转化为字符串

2024-12-27 01:42:32
推荐回答(3个)
回答1:

答案补充:中文啊,呵呵,好说,Chr改为ChrW就行了。
建一个Command1。
代码如下。
============
Dim bytesdata
Private Sub Command1_Click()
Dim i As Integer, s As String
bytesdata = Array(8, 0, 48, 49, 50, 51, 52, 53, 54, 55)
For i = LBound(bytesdata) To UBound(bytesdata)
s = s & ChrW(bytesdata(i))
Next
Print s
End Sub

回答2:

可以直接用函数实现;
参考实例如下:
Function bytes2BSTR(vIn)
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = strReturn
End Function

回答3:

注意字节值不能为0,适合单字节字符或双字节汉字:
Private Sub Command1_Click()
Dim sTemp As String
Dim yTemp(1 To 10) As Byte
yTemp(1) = &H8
yTemp(2) = &H1
yTemp(3) = &H48
yTemp(4) = &H49
yTemp(5) = &H50
yTemp(6) = &H51
yTemp(7) = &H52
yTemp(8) = &H53
yTemp(9) = &H54
yTemp(10) = &H55
sTemp = StrConv(yTemp, vbUnicode)
Text1 = sTemp
End Sub