Skip to content

Instantly share code, notes, and snippets.

@godie007
Created May 14, 2012 20:51
Show Gist options
  • Select an option

  • Save godie007/2696812 to your computer and use it in GitHub Desktop.

Select an option

Save godie007/2696812 to your computer and use it in GitHub Desktop.
package trabajo;
import java.io.*;
import javax.swing.*;
public class Proyecto {
// el metodo main inicia
public static void main(String arg[]) throws IOException {
String NumADividir;
String Resultado = "";
int bin[] = new int[100];
int size = 100, posStart = -1;
int x = 0, temp;
NumADividir = JOptionPane
.showInputDialog("Número entero a convertir ? ");
// verifica que la cadena no sea mayor a 8 caracteres
if (NumADividir.length() <= 8) {
// inicializa vector
for (x = 0; x < size; x++)
bin[x] = -1;
temp = Integer.parseInt(NumADividir);
bin[0] = temp % 2;
// toma los binarios
for (x = 1; x < size; x++) {
temp /= 2;
bin[x] = temp % 2;
}
// recorta para no tomar los ceros excedentes
for (x = size - 1; x >= 0; x--)
if (bin[x] == 1) {
posStart = x;
break;
}
// forma la cadena final
for (x = posStart; x >= 0; x--)
Resultado = Resultado + Integer.toString(bin[x]);
JOptionPane.showMessageDialog(null, "Binario: " + Resultado,
"Resultados", JOptionPane.PLAIN_MESSAGE);
} else {
JOptionPane.showMessageDialog(null,
"La cantidad contiene más de 8 digitos", "Error !!!!",
JOptionPane.PLAIN_MESSAGE);
}
System.exit(0); // terminar aplicacion de la ventana
} // fin del metodo main
} // fin de la clase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment