Results 1 to 7 of 7

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

Threaded View

  1. #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
  •  



Click Here to Expand Forum to Full Width