Convert String to Binary AND Binary To String Using Java......


Hi Friends this program will demonstrate the logic for "How To Convert Sting to Binary AND Binary To Original String" 



Save The file Binary.java



class Binary

{

public static void main(String[] args)

{

String msg = "Hello";   //coverted String...



               //conversion of String To Binary.......



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);

               

               //Conversion of Binary to Original String..........



                String origmsg = "";



int count = binary.length()/8;

     

char a[]=msg.toCharArray();

                int i=0;

     

for( i=0;i<count;i++)

               {

                    a[i]=msg.charAt(i);

           origmsg = origmsg.concat(String.valueOf(a[i]));

                }



System.out.println("original message : " + origmsg);

}

}








OUTPUT : 






'Hello' to binary: 0100100001100101011011000110110001101111


original message : Hello






I hope this logic will helpful to all...........


Comments

Popular posts from this blog

Why Mark Zuckerberg’s Choose 4

Error message when I run sudo: unable to resolve host (none)

Convert MBR to GPT