Here you can find the source of toByteArray(BitSet bits)
public static byte[] toByteArray(BitSet bits)
//package com.java2s; /**/*from www .j a v a2 s.c o m*/ * Project: ${puma-common.aid} * * File Created at 2012-6-24 * $Id$ * * Copyright 2010 dianping.com. * All rights reserved. * * This software is the confidential and proprietary information of * Dianping Company. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with dianping.com. */ import java.util.BitSet; public class Main { public static byte[] toByteArray(BitSet bits) { byte[] bytes = new byte[bits.length() / 8 + 1]; for (int i = 0; i < bits.length(); i++) { if (bits.get(i)) { bytes[bytes.length - i / 8 - 1] |= 1 << (i % 8); } } return bytes; } /** * */ public static byte[] toByteArray(byte num) { return new byte[] { num }; } public static byte[] toByteArray(short num) { final byte[] r = new byte[2]; for (int i = 0; i < 2; i++) { r[i] = (byte) (num >>> (8 - i * 8)); } return r; } public static byte[] toByteArray(int num) { final byte[] r = new byte[4]; for (int i = 0; i < 4; i++) { r[i] = (byte) (num >>> (24 - i * 8)); } return r; } public static byte[] toByteArray(long num) { final byte[] r = new byte[8]; for (int i = 0; i < 8; i++) { r[i] = (byte) (num >>> (56 - i * 8)); } return r; } }