Java Bit String From toBitString(final byte n)

Here you can find the source of toBitString(final byte n)

Description

Returns a String representation of the bits in a byte.

License

Open Source License

Parameter

Parameter Description
n the byte to be converted.

Return

a string representation of the argument in base 2.

Declaration

public static String toBitString(final byte n) 

Method Source Code

//package com.java2s;
/*/*from  w w  w . ja  va 2s. com*/
  (c) copyright
      
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
    
  This library 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
  Lesser General Public License for more details.
    
  You should have received a copy of the GNU Lesser General
  Public License along with this library; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA
 */

public class Main {
    /**
     * Constant to represent the big endian format (the format used internally
     * in Java).
     **/
    public static boolean BIG_ENDIAN = true;

    /**
     * Returns a String representation of the bits in a byte. The String
     * begins with the most significant bit in the byte. The bits are
     * represented as '0' and '1's in the String.
     *
     * @param n the byte to be converted.
     * @return a string representation of the argument in base 2.
     **/
    public static String toBitString(final byte n) {
        final int tmp = n;
        final StringBuffer sb = new StringBuffer(8);
        for (int i = 7; i >= 0; i--) {
            if ((tmp & (1 << i)) >> i == 1) {
                sb.append('1');
            } else {
                sb.append('0');
            }
        }
        return sb.toString();
    }

    /**
     * Returns a String representation of the bits in a byte array. The
     * String begins with the representation of the first byte in the array
     * (byte[0]). The most significant comes first in each byte. The bits are
     * represented by '0' and '1's in the String.
     *
     * @param ba the byte[] to be converted.
     * @return a string representation of the argument in base 2.
     **/
    public static String toBitString(final byte[] ba) {
        return toBitString(ba, null);
    }

    /**
     * Returns a String representation of the bits in a byte array. The
     * String begins with the representation of the first byte in the array
     * (byte[0]) Bytes are separated by byteSpace. The most significant comes
     * first in each byte. The bits are represented by '0' and '1's in the
     * String.
     *
     * @param ba the byte[] to be converted.
     * @param byteSpace the spacing to use between each byte.
     * @return a string representation of the argument in base 2.
     **/
    public static String toBitString(final byte[] ba, final String byteSpace) {
        final StringBuffer sb = new StringBuffer();
        for (int i = 0; i < ba.length; i++) {
            sb.append(toBitString(ba[i]));
            if (byteSpace != null && i < ba.length - 1) {
                sb.append(byteSpace);
            }
        }
        return sb.toString();
    }

    /**
     * Returns a String representation of the bits in a float. The String
     * begins with the representation of the "most" significant bit in the
     * float (the sign bit). The bits are represented by '0' and '1's in the
     * String.
     *
     * @param f the float to be converted.
     * @return a string representation of the argument in base 2.
     **/
    public static String toBitString(final float f) {
        return toBitString(toByteArray(f));
    }

    /**
     * Returns a String representation of the bits in a float. The String
     * begins with the representation of the "most" significant bit in the
     * float (the sign bit). The bits are represented by '0' and '1's in the
     * String.
     *
     * @param f the float to be converted.
     * @param byteSpace the spacing to use between each byte.
     * @return a string representation of the argument in base 2.
     **/
    public static String toBitString(final float f, final String byteSpace) {
        final byte[] ba = toByteArray(f);
        return toBitString(ba, byteSpace);
    }

    /**
     * Returns a String representation of the bits in an int. The String
     * begins with the representation of the "most" significant bit in the int
     * (the sign bit). The bits are represented by '0' and '1's in the
     * String.
     *
     * Note: The difference between this method and Integer.toBinaryString()
     * is that this one does not remove leading '0's.
     *
     * @param i the int to be converted.
     * @return a string representation of the argument in base 2.
     **/
    public static String toBitString(final int i) {
        return toBitString(toByteArray(i));
    }

    /**
     * Converts an int to a byte[4] array. The
     * first byte (0) in the returned array contain the most significant
     * eigth bits of the supplied int, ie big-endian.
     *
     * @param i the int to be converted.
     * @return a byte[4] array.
     **/
    public static byte[] toByteArray(final int i) {
        return toByteArray(i, BIG_ENDIAN);
    }

    /**
     * Converts an int to a byte[4] array. If
     * format is true the first byte (0) in the returned array contain the
     * least significant eigth bits of the supplied int, ie little-endian. if
     * format is false the conversion is big-endian.
     *
     * Note: the BIG_ENDIAN and LITTLE_ENDIAN should be used in the
     * format parameter.
     *
     * @param i the int to be converted.
     * @param format the wanted format of the retured byte array.
     * @return a byte[4] array.
     **/
    public static byte[] toByteArray(final int i, final boolean format) {
        final byte[] ba = new byte[4];
        if (format) { // little-endian
            ba[0] = (byte) (i & 0x000000ff);
            ba[1] = (byte) ((i & 0x0000ff00) >> 8);
            ba[2] = (byte) ((i & 0x00ff0000) >> (8 * 2));
            ba[3] = (byte) ((i & 0xff000000) >> (8 * 3));
        } else { // big-endian
            ba[0] = (byte) ((i & 0xff000000) >> (8 * 3));
            ba[1] = (byte) ((i & 0x00ff0000) >> (8 * 2));
            ba[2] = (byte) ((i & 0x0000ff00) >> 8);
            ba[3] = (byte) (i & 0x000000ff);
        }
        return ba;
    }

    /**
     * Converts a float to a byte[4] array by placing the bits
     * in the float into bytes and placing them after each other in the same
     * order as Java stores floats, ie big-endian.
     *
     * @param f the float to be converted.
     * @return the resulting value as an byte[4] array.
     **/
    public static byte[] toByteArray(final float f) {
        final int i = Float.floatToRawIntBits(f);
        return toByteArray(i);
    }

    /**
     * Converts a float to a byte[4] array by placing the bits
     * in the float into bytes and placing them after each other in the same
     * order as Java stores floats, ie big-endian.
     *
     * Note: the constants BIG_ENDIAN and LITTLE_ENDIAN (format) should be
     * used in the format parameter.
     *
     * @param f the float to be converted.
     * @param format the wanted format of the returned byte array.
     * @return the resulting value as an byte[4] array.
     **/
    public static byte[] toByteArray(final float f, final boolean format) {
        final int i = Float.floatToRawIntBits(f);
        return toByteArray(i, format);
    }
}

Related

  1. toBitString(byte value)
  2. toBitString(byte[] bytes)
  3. toBitString(byte[] bytes)
  4. toBitString(byte[] bytes, final int digitsPerGroup, final String groupSeparator)
  5. toBitString(byte[] data)
  6. toBitString(final byte[] b)
  7. toBitString(final byte[] b)
  8. toBitString(final double d)
  9. toBitString(final StringBuilder sb, final byte value, final int count2)