Java Byte Create toByteFromBin(String binSymbols)

Here you can find the source of toByteFromBin(String binSymbols)

Description

Transform a string of 8 bits into a byte

License

Open Source License

Parameter

Parameter Description
binSymbols A string of binary symbols of length 8

Return

The byte representing the input bit string

Declaration

public static byte toByteFromBin(String binSymbols) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static final String BINARY_STRING_PREFIX1 = "0b";
    public static final String BINARY_STRING_PREFIX2 = "0B";
    /** The regular expression for a binary string */
    public static final String BINARY_REGEXP = "((" + BINARY_STRING_PREFIX1 + ")|(" + BINARY_STRING_PREFIX2
            + ")){0,1}[0-1]{0,}";

    /**/*from   w  w  w .  java2  s  .com*/
     * Transform a string of 8 bits into a byte
     *
     * @param binSymbols
     *            A string of binary symbols of length 8
     * @return The byte representing the input bit string
     */
    public static byte toByteFromBin(String binSymbols) {
        if (isValidBin(binSymbols) == false) {
            throw new IllegalArgumentException("Illegal characters in bin string");
        }

        binSymbols = stripBinaryPrefix(binSymbols);

        if (binSymbols.length() > 8) {
            throw new IllegalArgumentException(
                    "More than 8 bits in input bit string, cannot convert to a single byte");
        }

        while (binSymbols.length() != 8) {
            binSymbols = "0" + binSymbols;
        }

        // make a corresponding bit out of each symbol in the input string
        //
        // we make a single bit by reading in the symbol, shifting it to the correct place
        // in the byte and zeroing the rest of the byte. If we add all 8 of these bytes together
        // we'll get the correct final byte value
        //
        // bit 0 is MSB, bit 7 is LSB
        byte bit0 = (byte) (((Integer.parseInt(binSymbols.substring(0, 1))) & 0x01) << 7);
        byte bit1 = (byte) (((Integer.parseInt(binSymbols.substring(1, 2))) & 0x01) << 6);
        byte bit2 = (byte) (((Integer.parseInt(binSymbols.substring(2, 3))) & 0x01) << 5);
        byte bit3 = (byte) (((Integer.parseInt(binSymbols.substring(3, 4))) & 0x01) << 4);
        byte bit4 = (byte) (((Integer.parseInt(binSymbols.substring(4, 5))) & 0x01) << 3);
        byte bit5 = (byte) (((Integer.parseInt(binSymbols.substring(5, 6))) & 0x01) << 2);
        byte bit6 = (byte) (((Integer.parseInt(binSymbols.substring(6, 7))) & 0x01) << 1);
        byte bit7 = (byte) ((Integer.parseInt(binSymbols.substring(7, 8))) & 0x01);

        return ((byte) (bit0 + bit1 + bit2 + bit3 + bit4 + bit5 + bit6 + bit7));
    }

    public static boolean isValidBin(final String binSymbols) {
        return (binSymbols.matches(BINARY_REGEXP));
    }

    public static String stripBinaryPrefix(String binSymbols) {
        if (binSymbols.startsWith(BINARY_STRING_PREFIX1)) {
            binSymbols = binSymbols.substring(BINARY_STRING_PREFIX1.length());
        } else if (binSymbols.startsWith(BINARY_STRING_PREFIX2)) {
            binSymbols = binSymbols.substring(BINARY_STRING_PREFIX2.length());
        }

        return (binSymbols);
    }
}

Related

  1. toByte(T value)
  2. toByteBE(int value, byte[] dst, int offset)
  3. toByteByAddress(String address)
  4. toByteByHex(String address)
  5. toByteColor(int i)
  6. toByteMatrix(Number[][] matrix)
  7. toByteObject(String value, Byte defaultValue)
  8. toByteUnit(long values)
  9. toByteValue(final int value)