Here you can find the source of toBinary(final byte[] bytes)
Parameter | Description |
---|---|
bytes | byte[] array |
public static String toBinary(final byte[] bytes)
//package com.java2s; /*// w w w . ja v a 2s . c o m * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ public class Main { /** * Turn a byte array into 1's and 0's (in a String) * * @param bytes byte[] array * @return binary as a String */ public static String toBinary(final byte[] bytes) { final StringBuilder sb = new StringBuilder(bytes.length * Byte.SIZE); for (int i = 0; i < Byte.SIZE * bytes.length; i++) { sb.append((bytes[i / Byte.SIZE] << i % Byte.SIZE & 0x80) == 0 ? '0' : '1'); } return sb.toString(); } }