String of bits to a char

P

Pete

Only just started using VB and I've got a problem, not sure how to
solve this one.

I'm receiving a string from a comm port containing hex values which
need to be converted into characters using 7 bit ASCII encoding.

For example I receive "82" as hex from the comm port. This would
translate to "10000010". Using just the first 7 bits this would give
me the character "A". How would I do this in VB .Net?

If I had multiple characters being transmitted in hex for example
"9124". Which would translate to "1001000100100100". Using a multiple
of 7 bits I end up with the characters "HI". What do I need to do to
get VB .Net to translate these hex values into characters?

Hope someone can help I'm getting V confused by convert functions.

Pete
 
O

One Handed Man \( OHM - Terry Burns \)

Actually the character is in ASCII is Decimal 65

Try this

MsgBox(Convert.ToChar(82)).ToString()
 
M

Mattias Sjögren

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
 
C

Cor Ligthert

Hi Pete,

The best advice I can give you when Mathias solution is not the one you are
looking is to ask this as well in the newsgroup
..
microsoft.public.dotnet.general.

Jon Skeet does always this kind of problems.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top