Sub StringToHexString(S As String, SpaceFlag As Boolean) As String
Dim A As Short
Dim HighNibble As Short
Dim LowNibble As Short
Dim DigitBase As Short
Dim OutChar(S.Length * 3) As Char 'make it big enough for spaces too
Dim PutPtr As Int = 0
For I = 0 To S.Length - 1
A = Asc(S.CharAt(I))
HighNibble = A / 16 'rounds down
LowNibble = A - HighNibble * 16
If HighNibble < 10 Then DigitBase = 48 Else DigitBase = 65 - 10 'to convert nibble to ASCII
OutChar(PutPtr) = Chr(DigitBase + HighNibble)
PutPtr = PutPtr + 1
If LowNibble < 10 Then DigitBase = 48 Else DigitBase = 65 - 10 'to convert nibble to ASCII
OutChar(PutPtr) = Chr(DigitBase + LowNibble)
PutPtr = PutPtr + 1
If SpaceFlag Then
OutChar(PutPtr) = Chr(32) 'space
PutPtr = PutPtr + 1
End If
Next
If SpaceFlag Then
Return CharsToString(OutChar, 0, S.Length * 3 - 1) 'trim trailing space
Else
Return CharsToString(OutChar, 0, S.Length * 2)
End If
End Sub