Java Bit Print printBitBinary(byte[] bytes)

Here you can find the source of printBitBinary(byte[] bytes)

Description

Transform bite array to bit representation

License

Apache License

Parameter

Parameter Description
bytes array of bites which will be interpreted to binary map

Return

string like this: 0110 1111 0101 1000

Declaration

public static String printBitBinary(byte[] bytes) 

Method Source Code

//package com.java2s;
/*/*from  www.ja v a  2  s .co  m*/
 * Copyright 2014 Lukas Benda <lbenda at lbenda.cz>.
 *
 * Licensed 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 {
    /** Transform bite array to bit representation
     * @param bytes array of bites which will be interpreted to binary map
     * @return string like this: 0110 1111 0101 1000 */
    public static String printBitBinary(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            String s = Integer.toBinaryString((b & 0xFF) + 0x100).substring(1);
            if (sb.length() > 0) {
                sb.append(" ");
            }
            if (s.length() > 4) {
                sb.append(s.substring(0, 4)).append(" ").append(s.substring(4, s.length()));
            } else {
                sb.append(s);
            }
        }
        return sb.toString();
    }
}

Related

  1. printBitboard(long bitboard)
  2. printBitFormat(int number)
  3. printBitLong(long A)
  4. printBits(byte[] bytes)