views:

307

answers:

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 byte[] = {0xff,0x3e,0x12,0x45,0x1d,0x11,0x2a,0x80,0x81,0x45,0x1d,0x11,0x2a,0x80,0x81} How do I convert it to array of BigInteger's and then be able to recover it back the original byte array safely?

ty in advance.

+4  A: 

Use BigInteger.toByteArray() and BigInteger(byte[]).

According to the javadoc, the latter ...

Translates a byte array containing the two's-complement binary representation of a BigInteger into a BigInteger. The input array is assumed to be in big-endian byte-order: the most significant byte is in the zeroth element.

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:

  1. When you convert from the byte array to a BigInteger, also make a note of the size of the byte array. This information is not encoded in the BigInteger value.

  2. When you convert from the BigInteger to a byte array, sign-extend the byte array out to the same length as the original byte array.

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 N based on memory usage rather than based on the mathematical algorithm you are trying to implement.

Stephen C
Yes, it's different I have arbitrary byte array length, and that is a kind of mess. Seems like I really have to write those extra transformations my own. I didn't really found them ready written anywhere with google for this particular case :(TY
PatlaDJ
If the byte-array is arbitrary data (which I'd guess it is), then this is definitely not a good idea. Leading `0x00` bytes (or `0xFF` bytes, if the entire value is negative) will be silently discarded when doing that.
Joachim Sauer
@PatiaDJ - I don't understand your point. Those methods **work** with arbitrary length byte arrays!!
Stephen C
Stephen, BigInteger(byte[]) will not create an array of BigIntegers. Which methods you refer, or I'm missing something here ?
PatlaDJ
@PatiaDJ - You are not much sense. In the question comments above, you said *"I dont want to convert any single byte to single BigInteger"*. But here you just said the opposite ... didn't you.
Stephen C
Stephen, I want to convert <strong>arbitrary</strong> size byte array, which represents whatever you can imagine: part of binary file, encoded message, or string... or...whatever you can imagine -> to <strong>an array of BigIntegers</strong>, and then be able to <strong>convert it back safely</strong> to the original byte array. Converting any single byte to a single BigInteger is not an option for me because it is over-utilizing memory, right?
PatlaDJ
So how is the conversion supposed to know how many bytes to load into each BigInteger?
Stephen C
Stephen, Isn't BigInteger fixed byte length ?
PatlaDJ
Stephen, I'll really appreciate yr help on this. I perform some tests now, and I see that BigInteger can grow very big and is not limited in size. That would have been enough for solution, if it didn't go too slow upon byte arrays bigger then 10k. So I am still facing the options to split it to n sized array of BigIntegers, or not to use BigInteger object at all.I use it because of the modPow method of BigInteger.Would you give me some suggestions?
PatlaDJ
Surely, if you are doing some calculations using these BigIntegers, then how you split them will determine the answers you get. Surely, your array size (`n`) MUST be determined by your algorithm ... not some arbitrary stuff to do with memory usage.
Stephen C
Thank you all!!
PatlaDJ
But yes, multiplication of BigInteger values has complexity `O(N**2)` where N is the number of bytes in the value. And modPow is based on multiplication.
Stephen C