Java Binary Encode toBinaryString(byte[] data, boolean format)

Here you can find the source of toBinaryString(byte[] data, boolean format)

Description

to Binary String

License

Open Source License

Declaration

public static String toBinaryString(byte[] data, boolean format) 

Method Source Code

//package com.java2s;
/* //from   w w  w . j a v  a  2 s.com
 * Copyright 2012 Devoteam http://www.devoteam.com
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * 
 * This file is part of Multi-Protocol Test Suite (MTS).
 * 
 * Multi-Protocol Test Suite (MTS) 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, either version 3 of the
 * License.
 * 
 * Multi-Protocol Test Suite (MTS) 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 Multi-Protocol Test Suite (MTS).
 * If not, see <http://www.gnu.org/licenses/>.
 * 
 */

public class Main {
    public static String toBinaryString(byte[] data) {
        return toBinaryString(data, 0, -1, 0, true);
    }

    public static String toBinaryString(byte[] data, boolean format) {
        return toBinaryString(data, 0, -1, 0, format);
    }

    public static String toBinaryString(byte[] data, int offset, int length, int indent) {
        return toBinaryString(data, offset, length, indent, true);
    }

    public static String toBinaryString(byte[] data, int offset, int length, int indent, boolean format) {
        String indentStr = indent(indent);
        String res = "";

        String string;
        string = "";

        if (length == -1) {
            length = data.length - offset;
        }

        for (int i = offset; i < offset + length; i++) {
            int value = data[i];

            if (value < 0) {
                value = 256 + value;
            }

            String octet = Integer.toHexString(value);
            if (octet.length() < 2) {
                octet = "0" + octet;
            }

            string += "h" + octet + " ";

            if (format && string.length() % (4 * 16) == 0) {
                res += indentStr + string + "\n";
                string = "";
            }
        }
        if (string.length() > 0) {
            res += indentStr + string;
        }

        return res;
    }

    /**
     * generates a string of nb*"    " (four spaces nb times), used for intentation in printAvp
     */
    public static String indent(int nb) {
        String str = "";
        for (int i = 0; i < nb; i++) {
            str += "    ";
        }
        return str;
    }
}

Related

  1. toBinaryString(byte... bytes)
  2. toBinaryString(byte[] array)
  3. toBinaryString(byte[] bytes)
  4. toBinaryString(byte[] bytes)
  5. toBinaryString(byte[] bytes, int bitSize)
  6. toBinaryString(char value)
  7. toBinaryString(final boolean[] bits)
  8. toBinaryString(final byte number)
  9. toBinaryString(final char[] arr)