Java Bit String From toBitString(byte[] bytes, final int digitsPerGroup, final String groupSeparator)

Here you can find the source of toBitString(byte[] bytes, final int digitsPerGroup, final String groupSeparator)

Description

to Bit String

License

Apache License

Declaration

static String toBitString(byte[] bytes, final int digitsPerGroup, final String groupSeparator) 

Method Source Code

//package com.java2s;
/**/* www. ja v a 2s  . c  o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    static final String[] BIT_STRINGS = new String[16];

    static String toBitString(byte[] bytes, final int digitsPerGroup, final String groupSeparator) {
        if (digitsPerGroup <= 0 || (digitsPerGroup & 7) != 0) {
            throw new IllegalArgumentException(
                    "digitsPerGroup <= 0 || (digitsPerGroup & 1) != 0, digitsPerGroup=" + digitsPerGroup);
        }
        final StringBuilder b = new StringBuilder();
        int r = 0;
        for (int i = bytes.length - 1; i >= 0; i--) {
            b.append(BIT_STRINGS[bytes[i] >>> 4]).append(BIT_STRINGS[bytes[i] & 0xF]);
            if ((r += 8) == digitsPerGroup && i >= 0) {
                r = 0;
                b.append(groupSeparator);
            }
        }
        return b.toString();
    }

    static String toBitString(long[] integers, final int digitsPerGroup, final String groupSeparator) {
        if (digitsPerGroup <= 0 || (digitsPerGroup & 3) != 0) {
            throw new IllegalArgumentException(
                    "digitsPerGroup <= 0 || (digitsPerGroup & 1) != 0, digitsPerGroup=" + digitsPerGroup);
        }
        final StringBuilder b = new StringBuilder();
        int r = 0;
        for (int i = integers.length - 1; i >= 0; i--) {
            for (int j = 28; j >= 0; j -= 4) {
                b.append(BIT_STRINGS[(int) (integers[i] >>> j) & 0xF]);
                if ((r += 4) == digitsPerGroup && i >= 0) {
                    r = 0;
                    b.append(groupSeparator);
                }
            }
        }
        return b.toString();
    }

    static String toString(int[] x) {
        if (x == null) {
            return "null";
        } else if (x.length == 0) {
            return "<empty>";
        } else {
            final StringBuilder b = new StringBuilder("[");
            b.append(x[0]);
            for (int i = 1; i < x.length; i++) {
                b.append(", ").append(x[i]);
            }
            return b.append("]").toString();
        }
    }

    static String toString(int[] x, int length, int M) {
        final int hexPerDigit = (M + 3) >> 2;
        final StringBuilder b = new StringBuilder();
        for (int i = length - 1; i >= 0; i--) {
            b.append(String.format(" %0" + hexPerDigit + "X", x[i]));
        }
        return b.toString();
    }
}

Related

  1. toBits(long value, int length)
  2. toBitsArray(byte[] bytes, int bitCount)
  3. toBitString(byte value)
  4. toBitString(byte[] bytes)
  5. toBitString(byte[] bytes)
  6. toBitString(byte[] data)
  7. toBitString(final byte n)
  8. toBitString(final byte[] b)
  9. toBitString(final byte[] b)