Here you can find the source of toByteArray(String bits)
public static byte[] toByteArray(String bits)
//package com.java2s; //License from project: LGPL import java.nio.ByteBuffer; public class Main { public static byte[] toByteArray(double value) { byte[] bytes = new byte[8]; ByteBuffer.wrap(bytes).putDouble(value); return bytes; }// www. j av a2 s . c o m public static byte[] toByteArray(String bits) { int byteLength = bits.length() / 8; if (bits.length() % 8 != 0) { byteLength += 1; } byte[] bytes = new byte[byteLength]; for (int i = 0; i < bits.length(); i++) { setBit(bytes, i, (byte) (bits.charAt(i) == '1' ? 1 : 0)); } return bytes; } public static void setBit(byte[] data, long pos, byte val) { int posByte = (int) (pos / 8); int posBit = (int) (pos % 8); byte oldByte = data[posByte]; data[posByte] = setBit(oldByte, posBit, val); } public static byte setBit(byte data, long pos, byte val) { data = (byte) (((0xFF7F >> pos) & data) & 0x00FF); return (byte) ((val << (8 - (pos + 1))) | data); } }