Advertisement
Guest User

Kristian

a guest
Aug 19th, 2007
3,221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.09 KB | None | 0 0
  1. Imports System.Text
  2. Imports System.Text.RegularExpressions
  3.  
  4. Public Class frmTest
  5.     Public Function TextToBinary(ByVal Text As String, Optional ByVal Separator As String = " ") As String
  6.  
  7.         Dim oReturn As New StringBuilder
  8.  
  9.         ' Convert to ASCII and go through all the bytes
  10.         For Each Character As Byte In ASCIIEncoding.ASCII.GetBytes(Text)
  11.             oReturn.Append(Convert.ToString(Character, 2).PadLeft(8, "0"))
  12.             oReturn.Append(Separator)
  13.         Next
  14.  
  15.         Return oReturn.ToString
  16.  
  17.     End Function
  18.  
  19.     Public Function BinaryToText(ByVal BinaryText As String) As String
  20.  
  21.         Dim Characters As String = Regex.Replace(BinaryText, "[^01]", "")
  22.         Dim ByteArray((Characters.Length / 8) - 1) As Byte
  23.  
  24.         ' Retrieve the original byte array
  25.         For Index As Integer = 0 To ByteArray.Length - 1
  26.             ByteArray(Index) = Convert.ToByte(Characters.Substring(Index * 8, 8), 2)
  27.         Next
  28.  
  29.         ' Convert the ASCII-array to a Unicode string
  30.         Return ASCIIEncoding.ASCII.GetString(ByteArray)
  31.  
  32.     End Function
  33. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement