Results 1 to 7 of 7

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

Threaded View

  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.

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
  •  



Click Here to Expand Forum to Full Width