Thursday 14 June 2018

Byte to Binary Conversion java program source code

Byte to Binary Conversion java program source code .this code convert Byte data to binary data after
compile this code enter a number and find your binary converted number immediately.
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javasample;
import java.io.*;
/**
 *
 * @author rajesh kumar shukla
 *

 writting them back out in binary representation.
*/
    public class BinaryConversion {
     BufferedInputStream brIn;
     PrintStream psOut;
    
     public static int BYTES_PER_LINE = 4;
    
         public BinaryConversion() {
         this(System.in,System.out);
         System.out.println("enter a nuber");
     }
    
         public BinaryConversion(InputStream in, OutputStream out) {
         brIn = new BufferedInputStream(in);
         if (out instanceof PrintStream)
         psOut = (PrintStream)out;
         else
         psOut = new PrintStream(out);
     }
    
     
    
         public void doit() {
         int ch, cv, bit, cnt;
             try {
                 for(cnt = 0, ch = brIn.read(); ch >= 0; ch = brIn.read()) {
                 cv = ((int)ch & 0x00ff);
                     for(bit = 7; bit >= 0; bit--) {
                     if ((cv & (2 << bit)) > 0)
                     psOut.print("1");
                     else
                     psOut.print("0");
                 }
                 cnt++;
                 if ((cnt % BYTES_PER_LINE) == 0) 
                 psOut.println("");
             }
         } catch (IOException e) { }
         return;
     }
    
     /**
     * Test main for BinCat 
     */
         public static void main(String args[]) {
         BinaryConversion kitty;
         kitty = new BinaryConversion();
         kitty.doit();
         System.exit(0);
     }
}

0 comments:

Post a Comment