How to change a String into a Byte to show a ASCII Character

T

Tobi

Hi,
i have a problem.
I want to get Ascii Caracters out of a 7 Bit long Byte, that should be
no problem with the getString Method, but i dont have a byte i have a
String which contains only an array of bits (i.e. 0010101).

Can anybody help me to change this string into a byte ?
Thank you,

Tobias
 
J

Jeremy Kitchen

Hi,
i have a problem.
I want to get Ascii Caracters out of a 7 Bit long Byte, that should be
no problem with the getString Method, but i dont have a byte i have a
String which contains only an array of bits (i.e. 0010101).

Can anybody help me to change this string into a byte ?
Thank you,

Tobias


Try
Convert.ToByte("0010101")
 
J

Jeremy Kitchen

Its not working, i get an overflow (Make sure your not dividing
through zero...)
any other ideas ?

Oops That creates trouble because it isn't treating it as a byte
array.

Try this function instead. You will want to add length checking etc.

Function GetChar(ByVal MybyteArray As String) As Char
Dim i As Integer = MybyteArray.Length - 1
Dim MyByte As Byte = 0
While i >= 0
If MybyteArray.Chars(i) = "1"c Then
MyByte = MyByte + 2 ^ i
End If
i -= 1
End While
Return Convert.ToChar(MyByte)
End Function
 
M

Mudhead

Convert.ToByte("0010101", 2)

Jeremy Kitchen said:
Oops That creates trouble because it isn't treating it as a byte
array.

Try this function instead. You will want to add length checking etc.

Function GetChar(ByVal MybyteArray As String) As Char
Dim i As Integer = MybyteArray.Length - 1
Dim MyByte As Byte = 0
While i >= 0
If MybyteArray.Chars(i) = "1"c Then
MyByte = MyByte + 2 ^ i
End If
i -= 1
End While
Return Convert.ToChar(MyByte)
End Function
 

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