We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Convert Binary to Text/String

techprocess
techprocess asked
on
Medium Priority
2,037 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
Comment
Watch Question

Dirk HaestProject manager
CERTIFIED EXPERT

Commented:
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

Project manager
CERTIFIED EXPERT
Commented:
Binary to Text Encode/Decode Class
http://www.codeproject.com/KB/security/TextCoDec.aspx

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.