Java Binary Encode toBinaryString(int value)

Here you can find the source of toBinaryString(int value)

Description

Helper.

License

Apache License

Parameter

Parameter Description
value to convert the value

Return

String binary representation.

Declaration

public static String toBinaryString(int value) 

Method Source Code

//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);
    }
}

Related

  1. toBinaryString(int a, int bits)
  2. toBinaryString(int b, int bits)
  3. toBinaryString(int l, int L)
  4. toBinaryString(int number)
  5. toBinaryString(int number, int length)
  6. toBinaryString(int[] input)
  7. toBinaryString(long val)
  8. toBinaryString(long value, int bitLegnth)
  9. toBinaryStringAddress(long address)