Obs.: Ela converte tanto de ascii p/ binário como faz o contrário.
public class Converter
{
public static void main(String[] args)
{
if (args[0] != null)
{
if (args[0].startsWith("0") || args[0].startsWith("1"))
{
try
{
System.out.println(bin2Dec(args[0]));
}
catch(StringIndexOutOfBoundsException e)
{
System.out.println(dec2Bin(args[0]));
}
}
else
{
StringBuffer textoAux = new StringBuffer();
textoAux.append(args[0]);
for(int n=1;n<args.length;n++)
{
textoAux.append(" " + args[n]);
}
String texto = textoAux.toString();
System.out.println(dec2Bin(texto));
}
System.out.println();
}
}
public static String dec2Bin(String texto)
{
StringBuffer bin = new StringBuffer();
for (int i = 0; i < texto.length(); i++)
{
int num = (texto.charAt(i));
for (int p = 7; p >= 0; p--)
{
bin.append((char) (((num >> p) & 1) + 48));
}
}
return bin.toString();
}
public static String bin2Dec(String bits) throws StringIndexOutOfBoundsException{
StringBuffer ascii = new StringBuffer();
for (int i = 0; i < bits.length(); i++)
{
if (i == 0 || i % 8 == 0)
{
try
{
char[] s = bits.substring(i, (i + 8)).toCharArray();
int num = 0;
for (int p = 0; p < 8; p++)
{
num += s[7 - p] == '1' ? (1 << p) : 0;
}
ascii.append((char) num);
}
catch(StringIndexOutOfBoundsException e)
{
throw e;
}
}
}
return ascii.toString();
}
}
Rodrigo Norÿfffff5es wrote:
:P 1000011 1110101 1110010 1110010 1101001 1100011 1110101 1101100 1110101 1101101 100000 1010110 1101001 1110100 1100001 11001011010010 1101111 1100100 1110010 1101001 1100111 1101111 100000 1001110 1101111 1110010 1101111 1100101 1110011 100000 1001101 1101111 1110101 1110010 1100001 100000 1100100 1100101 100000 1000011 1100001 1110010 1110110 1100001 1101100 1101000 1101111 101100 100000 110010 110001 100000 1100001 1101110 1101111 1110011 101100 100000 1110010 1101111 1100100 1110010 1101001 1100111 1101111 1011111 1101110 1101111 1110010 1101111 1100101 1110011 1000000 1111001 1100001 1101000 1101111 1101111 101110 1100011 1101111 1101101 101110 1100010 1110010 101110 100000 1000100 1100101 1110011 1100101 1101110 1110110 1101111 1101100 1110110 1100101 1100100 1101111 1110010 100000 1001010 1100001 1110110 1100001 101100 100000 1100011 1101111 1101101 100000 1100101 1111000 1110000 1100101 1110010 1101001 1100101 1101110 1100011 1101001 1100001 100000 1100101 1101101 100000 1001010 110010 1010011 1000101 101100 100000 1001010 110010 1000101 1000101 100000 1100101 100000 1001010 110010 1001101 1000101 101110 100000 1010000 1110010 1101111 1100111 1110010 1100001 1101101 1100001 1100100 1101111 1110010 100000 1100100 1100001 100000 1000010 1100101 1110010 1101101 1100001 1110011 100000 1001001 1101110 1100100 101110 100000 100110 100000 1000011 1101111 1101101 101110 100000 1001100 1010100 1000100 1000001 100000 1100001 100000 110010 100000 1100001 1101110 1101111 1110011 100000 1100101 100000 110101 100000 1101101 1100101 1110011 1100101 1110011 101100 100000 1100100 1100101 1110011 1100101 1101110 1110110 1101111 1101100 1110110 1100101 1101110 1100100 1101111 100000 1101111 100000 1000101 1010010 1010000 100000 1100100 1100001 100000 1100101 1101101 1110000 1110010 1100101 1110011 1100001 100000 1101110 1100001 1110011 100000 1101100 1101001 1101110 1100111 1110101 1100001 1100111 1100101 1101110 1110011 100000 1001001 1001100 1000101 100000 1010010 1010000 1000111 101100 100000 1000111 1100101 1101110 1100101 1111000 1110101 1110011 100000 1100101 100000 1001010 1100001 1110110 1100001 101110 100000 1000110 1101111 1110010 1101101 1100001 1100011 1100001 1101111 100000 1000001 1100011 1100001 1100100 1100101 1101101 1101001 1100011 1100001 111010 100000 1000011 1110101 1110010 1110011 1101111 111010 100000 1001001 1101110 1100110 1101111 1110010 1101101 1100001 1110100 1101001 1100011 1100001 100000 101101 100000 1010101 1001110 1001001 1000110 1001111 1010010 100000 101101 100000 1010001 1110101 1100001 1110010 1110100 1101111 100000 1010011 1100101 1101101 1100101 1110011 1110100 1110010 1100101 101110 --- "Anaximandro B. de Melo." <[EMAIL PROTECTED]> escreveu: >-------------------------------------------------------------------------------------------Ceara' Java User Group Para cancelar sua assinatura, envie um e-mail para: [EMAIL PROTECTED] Para mais informacoes, mande um e-mail para: [EMAIL PROTECTED] Falar com o administrador? e-mail para: [EMAIL PROTECTED]______________________________________________________________________ Yahoo! Messenger - Fale com seus amigos online. Instale agora! http://br.download.yahoo.com/messenger/ ------------------------------------------------------------------------------------------- Ceara' Java User Group Para cancelar sua assinatura, envie um e-mail para: [EMAIL PROTECTED] Para mais informacoes, mande um e-mail para: [EMAIL PROTECTED] Falar com o administrador? e-mail para: [EMAIL PROTECTED]
<<inline: logomail.gif>>