Binary String into Byte

  • Thread starter Thread starter Tobi
  • Start date Start date
T

Tobi

Hello,
I have a string containing a binary code,

mystring = "1010100"

and i want to write this string ino a byte, not an byte array to
interpretate it as an ASCII Letter.

I found a way to put it in a byte array, but then i have a bytearray
containing (49), (48) ,(49), (48) ,...
Thank you very much for your help
Tobias
 
Here is one way you can do it.

Private Function ToByte(ByVal s As String) As Byte
Dim b As Byte = 0
Dim digit As Byte = 2 ^ (s.Length - 1)
For Each c As Char In s
If c = "1"c Then
b = b Or digit
End If
digit = digit / 2
Next
Return b
End Function

====================
Clay Burch
Syncfusion, Inc.
 
I have a string containing a binary code,
mystring = "1010100"

and i want to write this string ino a byte, not an byte array to
interpretate it as an ASCII Letter.

I thought Mudhead had answered this perfectly in his post to your identical
thread that you started yesterday:

\\\
Dim b As Byte
b = Convert.ToByte("1010100", 2)
///

Was there something wrong with that solution?
 
Back
Top