You are on page 1of 4

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace otrobinariooctal
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void decToBin(long numero)
{
textBox2.Text = ""; //Esto debe hacerse para que no mezcle varios
resultados en la misma caja de texto.
if (numero > 1)
{
decToBin(numero / 2); //Llamado recursivo de la función
}
textBox2.AppendText((numero%2).ToString());
}
public void decToOct(long numero)
{
textBox2.Text = "";
if (numero > 7)
{
decToOct(numero / 8);
}
textBox2.AppendText((numero % 8).ToString());
}
public String octToDec(String cadNumero)
{
int i, iLength;
long iResult = 0;
iLength = cadNumero.Length;
long[] cadena = new long[cadNumero.Length];
long numero = long.Parse(cadNumero);
iLength--;
long cifra = 0;
for (int j = cadNumero.Length - 1; j >= 0; j--)
{
cifra = numero % 10; //saca la ultima cifra del numero
numero = numero / 10; //Guarda el numero menos la ultima cifra...
cadena[j] = cifra; //
}
for (i = 0; i < cadNumero.Length; i++, iLength--)
{
iResult += cadena[ i ] * (long)Math.Pow(8, iLength);
}
return iResult.ToString();
}
public String binToDec(String cadNumero)
{
int i, iLength;
long iResult = 0;
iLength = cadNumero.Length;
long[] cadena = new long[cadNumero.Length];
long numero = long.Parse(cadNumero);
iLength--;
long cifra = 0;
for (int j = cadNumero.Length - 1; j >= 0; j--)
{
cifra = numero % 10; //saca la ultima cifra del numero
numero = numero / 10; //Guarda el numero menos la ultima cifra...
cadena[j] = cifra; //
}
for (i = 0; i < cadNumero.Length; i++, iLength--)
{
iResult += cadena[ i ] * (long)Math.Pow(2, iLength);
}
return iResult.ToString();
}

private void button1_Click(object sender, EventArgs e)


{

if (radioButton1.Checked == true && radioButton4.Checked == true)


//Binario a Binario...
{
textBox2.Text = "Escoja Bases diferentes";
}
if (radioButton2.Checked == true && radioButton5.Checked == true)
//Decimal a decimal
{
textBox2.Text = "Escoja Bases diferentes";
}
if (radioButton3.Checked == true && radioButton6.Checked == true)
//Hexadecimal a Hexadecimal
{
textBox2.Text = "Escoja Bases diferentes";
}
if (radioButton1.Checked == true && radioButton5.Checked == true)
//Binario a Decimal
{
textBox2.Text = binToDec(textBox1.Text);
}
if (radioButton7.Checked == true && radioButton8.Checked == true)
//Octal a octal
{
textBox2.Text = "Escoja bases diferentes";
}
if (radioButton7.Checked == true && radioButton5.Checked == true)
//Octal a decimal
{
textBox2.Text = octToDec(textBox1.Text);
}
if (radioButton2.Checked == true && radioButton8.Checked == true)
//Decimal a Octal
{
decToOct(long.Parse(textBox1.Text));
}
if (radioButton1.Checked == true && radioButton8.Checked == true)
//Binario a Octal
{
String cadena = binToDec(textBox1.Text); //Guarda una cadena con el
numero decimal correspondiente
decToOct(long.Parse(cadena));//Aqui se supone que convierte de
Decimal a Octal y lo muestra en pantalla.
}
if (radioButton7.Checked == true && radioButton4.Checked == true)
//Octal a Binario
{
String cadena = octToDec(textBox1.Text); //Primero se lleva de octal
a decimal
decToBin(long.Parse(cadena)); //luego de decimal a binario
}
if (radioButton2.Checked == true && radioButton4.Checked == true)
//Decimal a Binario
{
decToBin(long.Parse(textBox1.Text));
}
if (radioButton3.Checked == true && radioButton5.Checked == true) //Hex
a Decimal
{
String texto1 = textBox1.Text;
int num = int.Parse(texto1,
System.Globalization.NumberStyles.HexNumber);
textBox2.Text = num.ToString();
}
if (radioButton2.Checked == true && radioButton6.Checked == true)
//Decimal a Hex
{
String texto1 = textBox1.Text;
int num = int.Parse(texto1);
String numHex = num.ToString("X");
textBox2.Text = numHex;
}
if (radioButton1.Checked == true && radioButton6.Checked == true)
//Binario a Hexadecimal
{
String cadNumero = binToDec(textBox1.Text); //Primero se hace
Binario a decimal
long numAux = long.Parse(cadNumero);
String numHex = numAux.ToString("X"); //Aqui hace decimal a
Hexadecimal...
textBox2.Text = numHex;
}
if (radioButton3.Checked == true && radioButton4.Checked ==
true)//Hexadecimal a Binario
{
long num = long.Parse(textBox1.Text,
System.Globalization.NumberStyles.HexNumber); //Aqui hace hexadecimal a decimal
decToBin(num); //Aqui hace decimal a binario
}
if (radioButton7.Checked == true && radioButton6.Checked == true)
//Octal a Hexadecimal
{
String cadena = octToDec(textBox1.Text); //Se guarda la cadena con
el numero decimal correspondiente
long numAux = long.Parse(cadena); //Se convierte en un numero long
textBox2.Text = numAux.ToString("X"); //Se lleva a hexadecimal y se
escribe en la segunda caja de texto.
}
if (radioButton3.Checked == true && radioButton8.Checked == true)
//Hexadecimal a Octal
{
long num = long.Parse(textBox1.Text,
System.Globalization.NumberStyles.HexNumber); //Hexadecimal a Decimal primero
decToOct(num); //De decimal a Octal y se escribe en la caja de
texto.
}
}

}
}
--------------------------------------------------------------------

public partial class Form1 : Form


{
public Form1()
{
InitializeComponent();
}
public void decToBin(long numero)
{
textBox2.Text = ""; //Esto debe hacerse para que no mezcle varios
resultados en la misma caja de texto.
if (numero > 1)
{
decToBin(numero / 2); //Llamado recursivo de la función
}
textBox2.AppendText((numero % 2).ToString());
}

private void button1_Click(object sender, EventArgs e)


{
decToBin(long.Parse(textBox1.Text));
}

}
}

You might also like