C# Binary To String

C# Binary To String

kick it on DotNetKicks.com


If you are trying to convert Text file into Binary file, please refer below link and use binary Writer.

http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx


   Some times we need to convert Binary to string or string to binary. It can be achieved by following technique.

This will help you to convert email attachments in to binary original file format.

1. Convert Binary data / file to string / text



protected void Button1_Click(object sender, EventArgs e)

{

Byte[] arrByte = { 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 };

string x = Convert.ToBase64String(arrByte);

Response.Write(x);

}
 


VB.Net code


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
   
   
    Dim arrByte As [Byte]() = {0, 1, 1, 0, 1, 0, _
    1, 0, 1, 0, 1, 0}
   
    Dim x As String = Convert.ToBase64String(arrByte)
   
       
    Response.Write(x)
End Sub
 



2. Convert String to Binary

 protected void Button1_Click(object sender, EventArgs e)

{
string str = "This is revenmerchantservices.com domain for ASp.net tips and tricks by Satalaj More";

 

byte []arr = System.Text.Encoding.ASCII.GetBytes(str);

 

}

 

Vb.Net Binary to string string to binary

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
   
    Dim str As String = "This is revenmerchantservices.com domain for ASp.net tips and tricks by Satalaj More"
     
    Dim arr As Byte() = System.Text.Encoding.ASCII.GetBytes(str)
End Sub


3. Convert binary to Base64 or Binary to string

  protected void Button1_Click(object sender, EventArgs e)

{

string str = "This is revenmerchantservices.com domain for ASp.net tips and tricks by Satalaj More";

 

byte []arr = System.Text.Encoding.ASCII.GetBytes(str); string base64String = Convert.ToBase64String(arr);

 

}

 

Vb.Net code

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
   
   
    Dim str As String = "This is revenmerchantservices.com domain for ASp.net tips and tricks by Satalaj More"
 
    Dim arr As Byte() = System.Text.Encoding.ASCII.GetBytes(str)
  
    Dim base64String As String = Convert.ToBase64String(arr) 

End Sub
 


All email attachments are in Base64 string format  that is 0-9, A-Z, a-z

Let me know your comments about this c# converting.

Currently rated 3.7 by 3 people

  • Currently 3.666667/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5