public static String byteArraytoBinary( byte[] bytes ){
StringBuilder sb = new StringBuilder(bytes.length * Byte.SIZE);
for( int i = 0; i < Byte.SIZE * bytes.length; i++ )
sb.append((bytes[i / Byte.SIZE] << i % Byte.SIZE & 0x80) == 0 ? '0' : '1');
return sb.toString();
}
public static byte[] binaryToByteArray( String s ){
int sLen = s.length();
byte[] toReturn = new byte[(sLen + Byte.SIZE - 1) / Byte.SIZE];
char c;
for( int i = 0; i < sLen; i++ )
if( (c = s.charAt(i)) == '1' )
toReturn[i / Byte.SIZE] = (byte) (toReturn[i / Byte.SIZE] | (0x80 >>> (i % Byte.SIZE)));
else if ( c != '0' )
throw new IllegalArgumentException();
return toReturn;
}
'Programming > Java' 카테고리의 다른 글
| 8자리 난수구하기 (0) | 2014.06.09 |
|---|---|
| 현재 시간 구하기 (0) | 2014.06.06 |
| 난수(Random Number) 출력 (0) | 2014.05.15 |
| (Java) String 반대로 순서 변경 (0) | 2014.04.16 |
