Java Byte to Binary byteToBinaryStr(byte b)

Here you can find the source of byteToBinaryStr(byte b)

Description

byte To Binary Str

License

Open Source License

Parameter

Parameter Description
b a parameter

Declaration

public static String byteToBinaryStr(byte b) 

Method Source Code

//package com.java2s;
/*/*from   w w  w .  j a  v a 2s. com*/
 * This file is part of trolCommander, http://www.trolsoft.ru/soft/trolcommander
 * Copyright (C) 2013-2014 Oleg Trifonov
 *
 * muCommander 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, or
 * (at your option) any later version.
 *
 * muCommander 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, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    private static final String[] STRING_OF_ZERO = { "", "0", "00", "000", "0000", "00000", "000000", "0000000",
            "00000000", "000000000", "0000000000" };
    private static final String[] BINARY_BYTE_STRINGS = new String[256];

    /**
     *
     * @param b
     * @return
     */
    public static String byteToBinaryStr(byte b) {
        int v = b & 0xFF;
        return byteToBinaryStr(v);
    }

    /**
     *
     * @param v
     * @return
     */
    public static String byteToBinaryStr(int v) {
        String result = BINARY_BYTE_STRINGS[v];
        if (result == null) {
            result = Integer.toBinaryString(v);
            result = STRING_OF_ZERO[8 - result.length()] + result;
            BINARY_BYTE_STRINGS[v] = result;
        }
        return result;
    }
}

Related

  1. byteToBin(byte[] bs)
  2. byteToBinary(byte value)
  3. byteToBinaryString(byte i)
  4. byteToBinaryString(byte toString)