VS 2008 Converting a "binary string" into a byte or char?-VBForums
Results 1 to 7 of 7

Thread: Converting a "binary string" into a byte or char?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2009
    Posts
    19

    Converting a "binary string" into a byte or char?

    I've been trying to figure this out for hours now, and I've given up on trying to solve it myself. Basically what I need to do, is convert a "double octet" binary string, such as "11010011 01011101", into a byte or char type if possible.

    I use the following code to convert my Unicode text into binary:
    Code:
        Private Function ByteToStr(ByVal bt() As Byte) As String
           Dim st As New StringBuilder
           For Each byt As Byte In bt
               st.Append(Convert.ToString(byt, 2).PadLeft(8, "0"c) & " ")
           Next
           Return st.ToString
        End Function
    
        Private Function TxtToBin(ByVal Str As String) As String
           Return ByteToStr(Encoding.Unicode.GetBytes(Str.ToCharArray))
        End Function
    And that seems to work perfectly, I get the results I'm after. But how do I then convert those results back into a string? So far, the only code that has worked for me is this:
    Code:
         Private Function BinToStr(ByVal BinStr As String)
            Dim str() As String = BinStr.Split(" "c)
            Dim bt As New List(Of Byte)
            For Each s As String In str
                For Each d As Byte In Convert.FromBase64String(s)
                    bt.Add(d)
                Next
            Next
            Return Encoding.Unicode.GetString(bt.ToArray)
        End Function
    But that gives me weird results. The "Convert.FromBase64String" obviously isn't what I'm after. Any help would be great. If I can convert the "binary string" into a bytes or chars I can get it into a string.

    EDIT: I suppose I'm dealing with the hexadecimal base, 16 bits? I really don't know much about base numbers or encoding...
    Last edited by PSYCHONAUT; May 23rd, 2010 at 03:29 PM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    30,804

    Re: Converting a "binary string" into a byte or char?

    The problem is that you don't really have a binary string, you just have a string. Each character is either a byte representing 1, a byte representing 0, or a byte representing a space. If it is unicode, then you would have two bytes for each, of course. Therefore, each byte is translated into 8 or 16 bytes, depending on what type of string. Now you want to take it all back. I'm not sure there is an efficient way to do this, as there often isn't an efficient way to work with strings. What you could do would be to take the string and split on spaces. This will give you an array of strings where each string is a single byte. You would then loop through each string, looking at each character, and adding it back into a byte. It would look something like this:
    Code:
    dim bt as Byte
    
    For x = 0 to 7
     If st1.Substring(x,1) = "1" then
      bt = (bt OR (1<<x))
     End If
    Next
    Not efficient, and not tested, but it should work for one byte. The same thing would have to be done for each byte in the whole string.
    My usual boring signature: Nothing

  3. #3
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Converting a "binary string" into a byte or char?

    For integers (and I changed your code a bit, SH):
    Code:
    Dim bt As Integer = 0
    Dim cpos As Integer = st1.Trim().Length
    For x = 0 to st1.Length - 1
         Select Case st1.Chars(x)
              Case "0"c
              Case "1"c
                   bt = bt Or (1 << cpos)
              Case Else
                   Continue For
         End Select
         cpos -= 1
    Next
    Remember, binary is RTL!

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2009
    Posts
    19

    Re: Converting a "binary string" into a byte or char?

    Ok, I've got this, I seem to be getting closer, but it's displaying different unicode characters than the ones I initially supplied:
    Code:
        Private Function BinToUni(ByVal BinStr As String)
            Dim stA() As String = BinStr.Split(" "c)
            Dim bl As New List(Of Byte)
            Dim bt As Byte = Nothing
            For c = 1 To stA.Length
                For x = 0 To 7
                    If stA(c - 1).Substring(x, 1) = "1" Then
                        bt = (bt Or (1 << x))
                    End If
                Next
                bl.Add(bt)
                bt = Nothing
            Next
            Return Encoding.Unicode.GetString(bl.ToArray)
        End Function
    I also tried the following:
    ok, I just tried the following:
    Code:
        Private Function BinToUni(ByVal BinStr As String)
            Dim stA() As String = BinStr.Split(" "c)
            Dim bl As New List(Of Byte)
            For Each s As String In stA
                bl.Add(Convert.ToByte(s))
            Next
            Return Encoding.Unicode.GetString(bl.ToArray)
        End Function
    But I keep getting this error: "Value was either too large or too small for an unsigned byte." That error happens for this line: bl.Add(Convert.ToByte(s))

  5. #5
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Converting a "binary string" into a byte or char?

    It thinks you want to store a value of something like one thousand one - "1001" - in a Byte.
    Code:
        Private Function BinToUni(ByVal BinStr As String)
            Dim stA() As String = BinStr.Split(" "c)
            Dim bl As New List(Of Byte)
            Dim bt As Byte = Nothing
            For c = 1 To stA.Length
                Dim bt As Integer = 0
                Dim cpos As Integer = stA(c-1).Trim().Length
                For x = 0 to stA(c-1).Length - 1
                     Select Case stA(c-1).Chars(x)
                          Case "0"c
                          Case "1"c
                               bt = bt Or (1 << cpos)
                          Case Else
                               Continue For
                     End Select
                     cpos -= 1
                Next
            Next
            Return Encoding.Unicode.GetString(bl.ToArray)
        End Function
    (SH's code doesn't work.)

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2009
    Posts
    19

    Re: Converting a "binary string" into a byte or char?

    minitech, thanks for the great help. I tried the following, but it returned gibberish, I must still be doing something wrong:

    Code:
           Private Function BinToUni(ByVal BinStr As String)
            Dim stA() As String = BinStr.Split(" "c)
            Dim bl As New List(Of Byte)
            For c = 1 To stA.Length
                Dim bt As Integer = 0
                Dim cpos As Integer = stA(c - 1).Trim().Length
                For x = 0 To stA(c - 1).Length - 1
                    Select Case stA(c - 1).Chars(x)
                        Case "0"c
                        Case "1"c
                            bt = bt Or (1 << cpos)
                        Case Else
                            Continue For
                    End Select
                    cpos -= 1
                Next
                bl.Add(bt)
            Next
            Return Encoding.Unicode.GetString(bl.ToArray)
        End Function
    EDIT: Oooops, there was a massive mistake there, I fixed it up, and changed the code above. I typed in "test", I converted it to binary, which gave me:
    Code:
    01110100 00000000 01100101 00000000 01110011 00000000 01110100 00000000
    I then tried converting it back to text and it gave me:
    Code:
    &#232;&#202;&#230;&#232;
    Last edited by PSYCHONAUT; May 23rd, 2010 at 05:59 PM.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2009
    Posts
    19

    Re: Converting a "binary string" into a byte or char?

    ok guys, never mind, the solution was actually simple all along, I could have sworn I tried it, but oh well. For anyone that might find it useful, here it is:

    Code:
        Private Function BinToTxt(ByVal BinStr As String)
            Dim stA() As String = BinStr.Split(" "c)
            Dim bl As New List(Of Byte)
            For Each s As String In stA
                bl.Add(Convert.ToByte(s, 2))
            Next
            Return Encoding.Unicode.GetString(bl.ToArray)
        End Function
    
        Private Function TxtToBin(ByVal Str As String) As String
            Dim bt() As Byte = Encoding.Unicode.GetBytes(Str.ToCharArray)
            Dim st As New StringBuilder
            For Each byt As Byte In bt
                st.Append(Convert.ToString(byt, 2).PadLeft(8, "0"c) & " ")
            Next
            Return st.ToString
        End Function
    
                'Example usage:
                'OutTxt.Text = TxtToBin(InTxt.Text)
                'OutTxt.Text = BinToTxt(InTxt.Text)
    EDIT: Updated code.
    Last edited by PSYCHONAUT; May 23rd, 2010 at 08:15 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.