[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Convert Binary to Text/String

Posted on 2009-02-12
2
Medium Priority
?
1,962 Views
Last Modified: 2012-05-06
I have a file which contains binary data I want to convert the binary data into proper understandable text
0
Comment
Question by:techprocess
  • 2
2 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 23630450
Convert binary file to text file and back to binary file
http://www.tanguay.info/webold/codeExample.php5?id=706
using System;
using System.IO;
 
namespace bi
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			EncodeBinaryFileToTextFile("test.mdb","encodedtestmdb.txt");
			DecodeTextFileFromBinaryFile("encodedtestmdb.txt","test3.mdb");
		}
 
		public static void EncodeBinaryFileToTextFile(string inputFileName, string outputFileName)
		{
			FileStream inFile;
			byte[] binaryData;
 
			//Convert file to binary file
			try
			{
				inFile = new System.IO.FileStream(inputFileName, System.IO.FileMode.Open,	System.IO.FileAccess.Read);
				binaryData = new Byte[inFile.Length];
				long bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);
				inFile.Close();
			}
			catch (System.Exception exp)
			{
				System.Console.WriteLine("{0}", exp.Message);
				return;
			}
 
			// Convert the binary input into Base64 UUEncoded output.
			string base64String;
			try
			{
				base64String = System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
			}
			catch (System.ArgumentNullException)
			{
				System.Console.WriteLine("Binary data array is null.");
				return;
			}
 
			// Write the UUEncoded version to the output file.
			System.IO.StreamWriter outFile;
			try
			{
				outFile = new System.IO.StreamWriter(outputFileName, false, System.Text.Encoding.ASCII);
				outFile.Write(base64String);
				outFile.Close();
			}
			catch (System.Exception exp)
			{
				System.Console.WriteLine("{0}", exp.Message);
			}
		}
 
 
 
 
		public static void DecodeTextFileFromBinaryFile(string inputFileName, string outputFileName)
		{
			System.IO.StreamReader inFile;
			string base64String;
 
			try
			{
				//get string from the text file
				char[] base64CharArray;
				inFile = new System.IO.StreamReader(inputFileName, System.Text.Encoding.ASCII);
				base64CharArray = new char[inFile.BaseStream.Length];
				inFile.Read(base64CharArray, 0, (int)inFile.BaseStream.Length);
				base64String = new string(base64CharArray);
			}
			catch (System.Exception exp)
			{
				System.Console.WriteLine("{0}", exp.Message);
				return;
			}
 
			// Convert the Base64 UUEncoded input into binary output.
			byte[] binaryData;
			try
			{
				binaryData = System.Convert.FromBase64String(base64String);
			}
			catch (System.ArgumentNullException)
			{
				System.Console.WriteLine("Base 64 string is null.");
				return;
			}
			catch (System.FormatException)
			{
				System.Console.WriteLine("Base 64 string length is not 4 or is not an even multiple of 4." );
				return;
			}
 
			// Write out the decoded data.
			System.IO.FileStream outFile;
			try
			{
				outFile = new System.IO.FileStream(outputFileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
				outFile.Write(binaryData, 0, binaryData.Length);
				outFile.Close();
			}
			catch (System.Exception exp)
			{
				System.Console.WriteLine("{0}", exp.Message);
			}
		}
 
	}
}

Open in new window

0
 
LVL 53

Accepted Solution

by:
Dhaest earned 1500 total points
ID: 23630453
Binary to Text Encode/Decode Class
http://www.codeproject.com/KB/security/TextCoDec.aspx
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question