• a

  •  

    julio 2008
    L M X J V S D
    « jun   ago »
     123456
    78910111213
    14151617181920
    21222324252627
    28293031  
  • Nube de Categoria

C#: Convertir Binario a Texto y Viceversa

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.

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

6 comentarios

  1. 01000011 01101111 01110010 01110010 01100101 01100011 01110100 01101111 00100001 00100001 00100001 00100000 01000101 01110011 01110100 01100101 00100000 01110100 01100101 01111000 01110100 01101111 00100000 01100101 01110011 01110100 01100001 00100000 01100011 01101111 01100100 01101001 01100110 01101001 01100011 01100001 01100100 01101111 00100000 01100101 01101110 00100000 01100010 01101001 01101110 01100001 01110010 01101001 01101111 00101110 00001101 00001010 01010000 01100001 01110010 01100001 00100000 01110000 01100001 01110011 01100001 01110010 00100000 01100001 01101100 00100000 01110011 01101001 01100111 01110101 01101001 01100101 01101110 01110100 01100101 00100000 01101110 01101001 01110110 01100101 01101100 00100000 01110101 01110011 01100001 00100000 01100011 01101111 01101101 01101111 00100000 01110010 01100101 01110011 01110000 01110101 01100101 01110011 01110100 01100001 00111010 00100000 01001111 01001110 01001111 01001101 01000001 01010100 01001111 01010000 01000101 01011001 01000001

  2. 01000011 01101111 01110010 01110010 01100101 01100011 01110100 01101111 00100001 00100001 00100001 00100000 01000101 01110011 01110100 01100101 00100000 01110100 01100101 01111000 01110100 01101111 00100000 01100101 01110011 01110100 01100001 00100000 01100011 01101111 01100100 01101001 01100110 01101001 01100011 01100001 01100100 01101111 00100000 01100101 01101110 00100000 01100010 01101001 01101110 01100001 01110010 01101001 01101111 00101110 00001101 00001010 01010000 01100001 01110010 01100001 00100000 01110000 01100001 01110011 01100001 01110010 00100000 01100001 01101100 00100000 01110011 01101001 01100111 01110101 01101001 01100101 01101110 01110100 01100101 00100000 01101110 01101001 01110110 01100101 01101100 00100000 01110101 01110011 01100001 00100000 01100011 01101111 01101101 01101111 00100000 01110010 01100101 01110011 01110000 01110101 01100101 01110011 01110100 01100001 00111010 00100000 01001111 01001110 01001111 01001101 01000001 01010100 01001111 01010000 01000101 01011001 01000001

  3. Hola, esta interesante, pero seria mejor, si explicaras mejor el codigo. Intento hacer un proyecto y necesito esto, solamente que lo que yo cargue de un textBox lo convierta a bin, y luego pueda hacer lo contrario.

  4. Pues C# lo puede hacer de manera automática jejeje… Con métodos de la clase System.Convert, saludos!!!

  5. Muy bueno, me ha dado ideas ya que esto es lo que buscaba

  6. Disculpa, pero creo que tienes algo de código que sobra:

    System.Console.WriteLine(“{0}”, exp.Message);
    Tambien funcinaria…
    System.Console.WriteLine(exp.Message);

    De esta forma te ahorras el “{0}”..en programación el objetivo no es hacer un programa que funcione, sino que haga la funcion para la que fue diseñado con el menor codigo posible

Deja un comentario

Fill in your details below or click an icon to log in:

Logo de WordPress.com

You are commenting using your WordPress.com account. Log Out / Cambiar )

Twitter picture

You are commenting using your Twitter account. Log Out / Cambiar )

Facebook photo

You are commenting using your Facebook account. Log Out / Cambiar )

Connecting to %s

Seguir

Get every new post delivered to your Inbox.