How to convert efficiently binary string to binary byte array in Java? -
as input have binary string string = "100110". output need have binary byte array byte[] b = {1,0,0,1,1,0}. i'm using
for (int i=0; i<a.length; i++) { b[i]= byte.parsebyte(a.substring(i, i+1)); }
but approach slow. can 1 give improve suggestion? give thanks you
you can without making objects substrings, this:
for (int i=0; i<a.length; i++) { b[i]= a.charat(i)=='1' ? (byte)1 : (byte)0; } the reason approach slower each phone call substring produces new string object, becomes eligible garbage collection parsebyte done it.
java arrays string binary-data
No comments:
Post a Comment