Convert strings and arrays of byte

The following Visual Basic functions can assist in converting strings and arrays of byte.

Public Sub Byte2String(InByte() As Byte, OutString As String)
  'Convert array of byte to string
   OutString = StrConv(InByte(), vbUnicode)
End Sub
 
Public Function String2Byte(InString As String, OutByte() As Byte) As Boolean
    'vb byte-array / string coercion assumes Unicode string
    'so must convert String to Byte one character at a time
	'or by direct memory access
 
    Dim I As Integer
    Dim SizeOutByte As Integer
    Dim SizeInString As Integer
 
    SizeOutByte = UBound(OutByte)
    SizeInString = Len(InString)
    'Verify sizes if desired
 
    'Convert the string
    For I = 0 To SizeInString - 1
      OutByte(I) = AscB(Mid(InString, I + 1, 1))
    Next I
   'If size byte array > len of string pad with Nulls for szString
   If SizeOutByte > SizeInString Then           'Pad with Nulls
      For I = SizeInString To SizeOutByte - 1
         OutByte(I) = 0
      Next I
   End If
 
   String2Byte = True
End Function
 
Public Sub ViewByteArray(Data() As Byte, Title As String)
   'Display message box showing hex values of byte array
 
   Dim S As String
   Dim I As Integer
   On Error GoTo VBANext
 
   S = "Length: " & Str(UBound(Data)) & "  Data (in hex):"
   For I = 0 To UBound(Data) - 1
      If (I Mod 8) = 0 Then
         S = S & "  "         'add extra space every 8th byte
      End If
      S = S & Hex(Data(I)) & " "
   VBANext:
   Next I
   MsgBox S, , Title
 
End Sub