String To Binary Conversion using java.....

Hi Friends, Here the logic for "String to Binary(8 bit)" conversion

Save the file Binary.java

class Binary
{
public static void main(String[] args)
{
String msg = "Hello";           //coverted String...

byte[] bytes = msg.getBytes();             //getting byte values

StringBuilder binary = new StringBuilder();

int msgg = 0;

for (byte b : bytes)
{
int val = b;
for (int i = 0; i < 8; i++)
{
binary.append((val & 128) == 0 ? 0 : 1);

   val <<= 1;

}

}

System.out.println("'" + msg + "' to binary: " + binary);
       }
}

I hope it is usefulll....


EmoticonEmoticon