Here you can find the source of toBinaryString(int value)
Parameter | Description |
---|---|
value | to convert the value |
public static String toBinaryString(int value)
//package com.java2s; /* Copyright 2013 Ivan Iljkic * * 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// w ww .ja v a 2s. c o m * * 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 { /** * Helper. Returns a String of the binary representation of the given value. * @param value to convert the value * @return String binary representation. */ public static String toBinaryString(int value) { return toBinaryString(value, 32); } /** * Helper. Returns a String of the binary representation of the given value. * @param value to convert the value * @return String binary representation. */ private static String toBinaryString(int value, int length) { String result = Integer.toBinaryString(value); StringBuilder buf = new StringBuilder(); for (int i = 0; (i + result.length()) < length; i++) { buf.append('0'); } buf.append(result); return buf.toString(); } /** * Helper. Returns a String of the binary representation of the given value. * @param value to convert the value * @return String binary representation. */ public static String toBinaryString(byte value) { return toBinaryString((value & 0xFF), 8); } /** * Helper. Returns a String of the binary representation of the given value. * @param value to convert the value * @return String binary representation. */ public static String toBinaryString(short value) { return toBinaryString((value & 0xFFFF), 16); } }