java - How to convert 160 digit binary string to 20 byte array? -
this question has answer here:
i have binary string containing 160 digit. have tried:
new biginteger("0000000000000000000000000000000000000000000000010000000000000000000000000000001000000000010000011010000000000000000000000000000000000000000000000000000000000000", 2).tobytearray()
but returns 15 bytes array removed leading 0 bytes.
i reserve leading 0 bytes, keep 20 bytes.
i know other ways accomplish that, know there easier way might need few lines of code.
why not simply:
public static byte[] convert160bitstobytes(string binstr) { byte[] = new biginteger(binstr, 2).tobytearray(); byte[] b = new byte[20]; int = 20 - a.length; int j = 0; if (i < 0) throw new illegalargumentexception("string long"); (; j < a.length; j++,i++) { b[i] = a[j]; } return b; }
Comments
Post a Comment