Here you can find the source of toByteArray(BitSet bits)
Parameter | Description |
---|---|
bits | Array of bits |
public static byte[] toByteArray(BitSet bits)
//package com.java2s; //License from project: Open Source License import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.*; public class Main { /**//from w w w.j av a2s . c o m * Convert short array to byte array * @param arr Array of shorts * @return Array of bytes */ public static byte[] toByteArray(short[] arr) { byte[] byteBlocks = new byte[arr.length * 2]; ByteBuffer.wrap(byteBlocks).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(arr); return byteBlocks; } /** * Convert BitSet to byte array * @param bits Array of bits * @return Array of bytes */ public static byte[] toByteArray(BitSet bits) { byte[] result = new byte[(bits.length() + 7) / 8]; for (int i = 0; i < bits.length(); ++i) { if (bits.get(i)) { result[i / 8] |= 1 << (i % 8); } } return result; } }