Jay Taylor's notes
back to listing indexjava - Convert arbitrary size of byte[] to BigInteger[] and then safely convert back to exactly the same byte[], any clues? - Stack Overflow
[web search]
1
|
I believe conversion exactly to BigInteger[] would be optimal in my case. Anyone had done or found this written in Java and willing to share? So imagine I have arbitrary size ty in advance.
| ||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||
5
|
Use BigInteger.toByteArray() and BigInteger(byte[]). According to the javadoc, the latter ...
If your byte-wise representation is different, you may need to apply some extra transformations. EDIT - if you need to preserve leading (i.e. non-significant) zeros, do the following:
EDIT 2 - if you want to turn a byte array into an array of BigIntegers with at most N bytes in each one, you need to create a temporary array of size N, repeatedly 1) fill it with bytes from the input byte array (with left padding at the end) and 2) use it to create BigInteger values using the constructor above. Maybe 20 lines of code? But I'm frankly baffled that you would (apparently) pick a value for
| ||||||||||||||||||||||||||||||||
|