Here you can find the source of toBitString(final byte[] b)
Parameter | Description |
---|---|
b | The unsigned byte[]. |
Parameter | Description |
---|---|
IllegalArgumentException | if the argument is <code>null</code>. |
public static String toBitString(final byte[] b)
//package com.java2s; /**/*from ww w.j ava 2 s . c o m*/ Copyright (C) SYSTAP, LLC 2006-2007. All rights reserved. Contact: SYSTAP, LLC 4501 Tower Road Greensboro, NC 27410 licenses@bigdata.com This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ public class Main { /** binary digits. */ private final static char[] bits = { '0', '1' }; /** * Return the binary representation of the unsigned byte[]. * * @param b * The unsigned byte[]. * * @return The representation of the bits in that unsigned byte[]. * * @throws IllegalArgumentException * if the argument is <code>null</code>. */ public static String toBitString(final byte[] b) { if (b == null)// Note: fromKey/toKey may be null; caller must check 1st throw new IllegalArgumentException(); final char[] chars = new char[b.length << 3]; // one char per bit. int bitIndex = 0; // start at the msb. for (int i = 0; i < b.length; i++) { final byte x = b[i]; // next byte. for (int withinByteIndex = 7; withinByteIndex >= 0; withinByteIndex--) { final int mask = 1 << withinByteIndex; final boolean bit = (x & mask) != 0; chars[bitIndex++] = bits[bit ? 1 : 0]; } // next bit in the current byte. } // next byte in the byte[]. // System.err.println("b[]=" + BytesUtil.toString(b) + ", chars=" // + Arrays.toString(chars)); return new String(chars); } }