java 비트 관련

개발 2022. 7. 25. 14:23 |
//byte[] -> long
lValue = ByteBuffer.wrap(asciiBytes).getLong();
lValue = new BigInteger(asciiBytes).longValue();

 

//byte[] -> bit ->binary String
byte[] asciiBytes = aaa.getBytes(StandardCharsets.US_ASCII);
BitSet bitset = BitSet.valueOf(asciiBytes);
Log.d(TAG,"bitAscii:"+bitset.toString());
Log.d(TAG,"Length of bitset = " + bitset.length());
Log.d(TAG,"Size of bitset = " + bitset.size()); //이걸써야
String bitAscii = "";
for (int i = 0 ; i < bitset.size()/8 ; ++i) {
    bitAscii = bitAscii + String.format("%8s", Integer.toBinaryString(bitset.toByteArray()[i] & 0xFF)).replace(' ', '0');
}

Log.d(TAG,"bitAscii1:"+bitAscii);
// 안됨
String bitAscii2 = "";
for (int i = 0 ; i < bitset.size(); ++i) {
    //System.out.print("bit " + i + ": " + bitset.get(i));
    bitAscii2 = bitAscii2 + ((bitset.get(i))?1:0);
}

Log.d(TAG,"bitAscii2:"+bitAscii2);
Posted by 흰털너부리
: