Para este articulo, necesitaremos abrir un proyecto Windows en CSharp.

Le agregaremos al formulario 2 botones: “Binario a Texto” (btnBinText_Click) y “Texto a Binario” (btnTextBin_Click) .. que original …

Finalmente agregaremos una clase que tendra los metodos para convertir nuestro archivo binario a texto y viceversa

Esta clase la denominaremos “bi.cs”.

Esta tendra 2 metodos: EncodeBinaryFileToTextFile y DecodeTextFileFromBinaryFile. Y sus definiciones son las siguientes:

public static void EncodeBinaryFileToTextFile(string inputFileName, string outputFileName)

public static void DecodeTextFileFromBinaryFile(string inputFileName, string outputFileName)

El codigo de esta clase es este:

    class bi
    {

    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);
    }
    }

    }

Para terminar, agregaremos el codigo necesario a nuestros botones para que llamen a los metodos correspondientes:

private void btnBinText_Click(object sender, EventArgse) 
{
	bi.EncodeBinaryFileToTextFile(“C:\\WINDOWS\\system32\\winmine.exe”, “winmine.txt”);

	MessageBox.Show(“conversion binario a texto ok!”);
}

private void btnTextBin_Click(object sender, EventArgse)
{
	bi.DecodeTextFileFromBinaryFile(“C:\\ConvertBinaryText\\ConvertBinaryText\\bin\\Debug\\winmine.txt”, “juego.exe”);

	MessageBox.Show(“conversion texto a binario ok!”);
}

Para este ejemplo, usamos el EXE del juego BuscaMinas (Winmine).

Via | http://www.tanguay.info/webold/codeExample.php5?id=706

private