public static void main(String[] args) {
BinaryPrint.getIns().putByte((byte)120).show();
BinaryPrint.getIns().putBytes(new byte[] {(byte)70,(byte)3,(byte)9}).show();
BinaryPrint.getIns().putChar('z').show();
BinaryPrint.getIns().putShort((short)1350).show();
BinaryPrint.getIns().putInt(1972).show();
BinaryPrint.getIns().putFloat(13.5F).show();
BinaryPrint.getIns().putLong(Long.MAX_VALUE).show();
BinaryPrint.getIns().putDouble(1.234e200d).show();
BinaryPrint.getIns().putByte((byte)120).putChar('z').putShort((short)1350).putInt(1972).show();
}
01111000
01000110 00000011 00001001
00000000 01111010
00000101 01000110
00000000 00000000 00000111 10110100
01000001 01011000 00000000 00000000
01111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111
01101001 01111001 11001011 01000000 11110101 11011110 01000011 10011011
01111000 00000000 01111010 00000101 01000110 00000000 00000000 00000111 10110100
/**
*
* @author : emunhi
* @date : 2017. 5. 5.
*
*/
package com.emunhi.util;
import java.nio.ByteBuffer;
public class BinaryPrint {
private StringBuilder sb = new StringBuilder();
/**
* 인스턴스를 생성한다.
* @return
*/
public static BinaryPrint getIns() {
return new BinaryPrint();
}
/**
* 입력한 값들의 2진수 문자열을 취득한다.
* @return 2진문자열
*/
public String get() {
return sb.toString();
}
/**
* 입력한 값들을 2진문자열로 출력한다.
*/
public void show() {
System.out.println(sb.toString());
}
public BinaryPrint putByte(byte value) {
if(!sb.toString().isEmpty()) sb.append(" ");
sb.append(bytes2BinaryString(ByteBuffer.allocate(1).put(value).array()));
return this;
}
public BinaryPrint putBytes(byte[] value) {
if(!sb.toString().isEmpty()) sb.append(" ");
sb.append(bytes2BinaryString(ByteBuffer.allocate(value.length).put(value).array()));
return this;
}
public BinaryPrint putChar(char value) {
if(!sb.toString().isEmpty()) sb.append(" ");
sb.append(bytes2BinaryString(ByteBuffer.allocate(2).putChar(value).array()));
return this;
}
public BinaryPrint putShort(short value) {
if(!sb.toString().isEmpty()) sb.append(" ");
sb.append(bytes2BinaryString(ByteBuffer.allocate(2).putShort(value).array()));
return this;
}
public BinaryPrint putInt(int value) {
if(!sb.toString().isEmpty()) sb.append(" ");
sb.append(bytes2BinaryString(ByteBuffer.allocate(4).putInt(value).array()));
return this;
}
public BinaryPrint putFloat(float value) {
if(!sb.toString().isEmpty()) sb.append(" ");
sb.append(bytes2BinaryString(ByteBuffer.allocate(4).putFloat(value).array()));
return this;
}
public BinaryPrint putLong(long value) {
if(!sb.toString().isEmpty()) sb.append(" ");
sb.append(bytes2BinaryString(ByteBuffer.allocate(8).putLong(value).array()));
return this;
}
public BinaryPrint putDouble(double value) {
if(!sb.toString().isEmpty()) sb.append(" ");
sb.append(bytes2BinaryString(ByteBuffer.allocate(8).putDouble(value).array()));
return this;
}
private BinaryPrint() {}
private static String bytes2BinaryString(byte[] bytes) {
StringBuffer sb = new StringBuffer();
int size = bytes.length;
for(int ii=0;ii<size;ii++) {
byte i = bytes[ii];
String byteInBinary = String.format("%8s", Integer.toBinaryString((i+256)%256)).replace(' ', '0');
if(ii>0) sb.append(" ");
sb.append(byteInBinary);
}
return sb.toString();
}
}