Here you can find the source of toBytesFromBin(String binSymbols)
Parameter | Description |
---|---|
binSymbols | The binary string to convert |
public static byte[] toBytesFromBin(String binSymbols)
//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 ww . j ava2 s .c o m * Given a string of binary symbols, convert it to an array of bytes * * @param binSymbols * The binary string to convert * @return The byte array corresponding to the input string */ public static byte[] toBytesFromBin(String binSymbols) { if (binSymbols == null || binSymbols.trim().length() == 0) { return (new byte[0]); } else if (isValidBin(binSymbols) == false) { throw new IllegalArgumentException("Invalid bin string specified"); } binSymbols = stripBinaryPrefix(binSymbols); while ((binSymbols.length() % 8) != 0) { binSymbols = "0" + binSymbols; } // 8 bits in a byte int numBytes = binSymbols.length() / 8; byte[] bytes = new byte[numBytes]; // loop through all the input symbols for (int i = 0; i < bytes.length; i++) { bytes[i] = toByteFromBin(binSymbols.substring(i * 8, (i * 8) + 8)); } return (bytes); } 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); } /** * 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)); } }