Binary String into Byte

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tobi

    #1

    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

  • ClayB

    #2
    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.

    Comment

    • Oenone

      #3
      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


      Comment

      Working...