Pete,
Here's how I'd do it (or at least my first attempt, it certainly isn't
fully tested or optimized in any way):
Class Test
Shared Sub Main()
PrintHexAs7BitChars( "9124" )
' PrintHexAs7BitChars( "82" )
End Sub
Shared Sub PrintHexAs7BitChars(hexstr As String)
Dim buf As New CharBuffer(10) ' up to 6 carryover bits + 4 new
For Each c As Char In hexstr.Toupper()
Dim val As Integer = AscW(c) - AscW("0"c)
If val > 9 Then val -= 7
System.Diagnostics.Debug.Assert(val >= 0 AndAlso val <= 15)
buf.WriteString(bin(val))
If buf.Count >= 7 Then
Dim firstSevenBits As String = buf.ReadString(7)
Dim asciiVal As Integer = Convert.ToInt32(firstSevenBits, 2)
Console.Write(ChrW(asciiVal))
End If
Next
Console.WriteLine()
End Sub
Shared ReadOnly bin() As String = {"0000", "0001", "0010", "0011", _
"0100", "0101", "0110", "0111", _
"1000", "1001", "1010", "1011", _
"1100", "1101", "1110", "1111"}
End Class
Structure CharBuffer
Private chars() As Char
Private r, w, n As Integer
Public Sub New(size As Integer)
chars = New Char(size - 1) {}
End Sub
Public Sub WriteChar(c As Char)
n += 1
chars(w) = c
w = (w + 1) Mod chars.Length
End Sub
Public Sub WriteString(s As String)
For Each c As Char In s
WriteChar(c)
Next
End Sub
Public Function ReadChar() As Char
n -= 1
Dim c As Char = chars(r)
r = (r + 1) Mod chars.Length
Return c
End Function
Public Function ReadString(n As Integer) As String
Dim buf(n - 1) As Char
For i As Integer = 0 To n - 1
buf(i) = ReadChar()
Next
Return New String(buf)
End Function
Public ReadOnly Property Count As Integer
Get
Return n
End Get
End Property
End Structure
Mattias
--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ |
http://www.dotnetinterop.com
Please reply only to the newsgroup.