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.

Tags:

C# Binary To StringReplace string in C#[C#] Remove Last Character From StringC# foreach loopHow To Declare and access array of string in C#c# stringSplit String In C#.net String vs stringAsp.net Convert String To ImageC# Convert String To DateTimeConvert Array To Comma Separated String C#

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.