Hi Nik,
I was going to reply to your ASCII Terminal query, but you've reposted.
Such impatience!! ;-)
|| Is this possible without a long, complicated lookup table?
Yes, lol. You use a short, uncomplicated table!
I presume that you're getting hex values, so '0'-'9', 'A'-'F' are the only
possibilities. Rather than me explaining, try the code below.
If 'a' to 'f' are possibilities, add them to the table.
Regards,
Fergus
<code>
Public Module AsciiToBitArrayConversion
Private CharToNibble() As Byte
Private Sub InitAsciiToBitArray
ReDim CharToNibble(256)
'These values are reversed because
'New BitArray (Byte()) reverses the bytes.
CharToNibble (Asc("0")) = &h0
CharToNibble (Asc("1")) = &h8
CharToNibble (Asc("2")) = &h4
CharToNibble (Asc("3")) = &hC
CharToNibble (Asc("4")) = &h2
CharToNibble (Asc("5")) = &hA
CharToNibble (Asc("6")) = &h6
CharToNibble (Asc("7")) = &hE
CharToNibble (Asc("8")) = &h1
CharToNibble (Asc("9")) = &h9
CharToNibble (Asc("A")) = &h5
CharToNibble (Asc("B")) = &hD
CharToNibble (Asc("C")) = &h3
CharToNibble (Asc("D")) = &hB
CharToNibble (Asc("E")) = &h7
CharToNibble (Asc("F")) = &hF
End Sub
Public Sub TestAtoB
Dim S As String = "80FF7BC30123456789ABCDEF"
Dim Bits As BitArray = AsciiToBitArray (S)
Dim I As Integer
Dim sBytes As String = ""
For I = 0 To Bits.Length - 1
If I / 4 = I \ 4 Then sBytes = sBytes & " "
If I / 8 = I \ 8 Then sBytes = sBytes & " "
sBytes = sBytes & DirectCast _
(IIF (Bits(I) = True, "1", "0"), String)
Next
Console.WriteLine (S & vbCrLf & sBytes)
S = S & ", " & SBytes
End Sub
Public Function AsciiToBitArray (S As String) As BitArray
If CharToNibble Is Nothing Then
InitAsciiToBitArray
End If
Dim aBytes (1 + S.Length \ 2) As Byte
Dim I As Integer
For I = 1 To S.Length Step 2
Dim Hi As Integer = CharToNibble (Asc (Mid (S, I + 1, 1)))
Dim Lo As Integer = CharToNibble (Asc (Mid (S, I + 0, 1)))
aBytes (I \ 2) = CByte (Hi * 16 + Lo)
Next
Return New BitArray (aBytes)
End Function
End Module
</code>