convert byte array to string and from string to byte array

convert byte array to string and from string to byte array

Post by Logan McKi » Wed, 11 Feb 2004 05:56:03

I have an application that interfaces with a third party remote control that
needs and returns the remote codes as a byte array. I need to be able to
save the codes in a text file which I load into a collection when you load
the program then write back to a file when you close the program. this
means I need to accept the code from the dll as a byte array then convert it
to a string and save it in the collection and convert it from the string (in
the collection) back to a byte array when the user wants to send that code.
Since byte array I pass into the capture and transmit functions are ByRef
does the Byte array have to be defined?

------ code that converts the byte array to string -------
'CaptureString is a byte array and is already set to the correct value
Dim sCode as String

Debut.Print CaptureString
sCode = CaptureString
Debug.Print sCode ' it appears correct in the Immediate window

But when I read it out of the string it is different
I have tried converting the string to byte array two different ways
-----way 1-----
For iii = 1 To CaptureStringSize
sTmp = Mid(sInfo, iii, 1)
iTmp = Asc(sTmp)
bTmp = CByte(iTmp)
CaptureString(iii - 1) = bTmp
-----way 2---- 'skip the even bytes (Unicode?)
bArray = sInfo
For iii = 0 To UBound(bArray)
If iii Mod 2 = 0 Then
iCount = iCount + 1
CaptureString(iCount) = bArray(iii)
End If
neither of which work or match the original CaptureString in the immediate
I am really lost and out of my element with vb so any help would be greatly

convert byte array to string and from string to byte array

Post by Veig » Wed, 11 Feb 2004 06:06:22

String to Byte Array
Dim b() As Byte
Dim s As String
s = "This is my String"
b = StrConv(s, vbFromUnicode)

Byte Array to String
sString = StrConv(yBytes, vbUnicode)

You could also loop through the byte array and convert each item in the
array to a character using the Chr$ function

Chris Hanscom
MVP (Visual Basic)
Application Design Section