PC Review Forums Newsgroups Microsoft DotNet Microsoft VB .NET Binary String into Byte

Reply

Binary String into Byte

 
Thread Tools Rate Thread
Old 14-03-2007, 12:48 PM   #1
Tobi
Guest
 
Posts: n/a
Default Binary String into Byte


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

  Reply With Quote
Old 14-03-2007, 01:18 PM   #2
ClayB
Guest
 
Posts: n/a
Default Re: Binary String into Byte

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.

  Reply With Quote
Old 14-03-2007, 03:55 PM   #3
Oenone
Guest
 
Posts: n/a
Default Re: Binary String into Byte

> 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?

--

(O)enone


  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off