Posts Tagged ‘Binary to String

28
Mar
09

[VB.Net] Binary To String Conversion

    Public Function BinaryToString(ByVal Binary As String) As String
        Dim Characters As String = System.Text.RegularExpressions.Regex.Replace(Binary, "[^01]", "")
        Dim ByteArray((Characters.Length / 8 ) - 1) As Byte
        For Index As Integer = 0 To ByteArray.Length - 1
            ByteArray(Index) = Convert.ToByte(Characters.Substring(Index * 8, 8), 2)
        Next
        Return System.Text.ASCIIEncoding.ASCII.GetString(ByteArray)
    End Function

Usage:

Debug.WriteLine(BinaryToString("01110011 01101001 01101101 00110000 01101110"))

Would output:

sim0n

Debug.WriteLine(BinaryToString("01110011,01101001,01101101,00110000,01101110,"))

Would output:

sim0n