Assuming your binary string can be split into 8 without rest, you can use the following method:
public static byte[] getByteByString(String binaryString){ Iterable iterable = Splitter.fixedLength(8).split(binaryString); byte[] ret = new byte[Iterables.size(iterable) ]; Iterator iterator = iterable.iterator(); int i = 0; while (iterator.hasNext()) { Integer byteAsInt = Integer.parseInt(iterator.next().toString(), 2); ret[i] = byteAsInt.byteValue(); i++; } return ret; }
Remember to add guava lib to your dependencies.
On Android, you should add gradle to the application:
compile group: 'com.google.guava', name: 'guava', version: '19.0'
And add this to your gradle project:
allprojects { repositories { mavenCentral() } }
Update 1
This post contains a solution without using Guava Lib.
lidox source share