Binary String into Byte

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
 
C

ClayB

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.
 
O

Oenone

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?
 

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