Reputation: 23
Is it possible to convert an ArrayList of BigIntegers to a byte array? If so, how?
_randomNumbers = new ArrayList(_size);
I tried these
foreach (BigInteger number in _randomNumbers)
{
bytes = number.ToByteArray();
}
Upvotes: 2
Views: 816
Reputation: 460098
I would use a List<BigInteger>
in the first place. There's no need for an ArrayList
anymore. Then you can use this LINQ query:
var bigIntegers = new List<System.Numerics.BigInteger>(); // fill...
byte[][] allByteArrays = bigIntegers.Select(bi => bi.ToByteArray()).ToArray();
Otherwise you need to cast every object in the ArrayList
:
byte[][] allByteArrays = bigIntegersArrayList.Cast<BigInteger>()
.Select(bi => bi.ToByteArray())
.ToArray();
For the sake of completeness, the classic way without LINQ which can be more efficient:
byte[][] allByteArrays = new byte[bigIntegers.Count][];
for (int i = 0; i < allByteArrays.Length; i++)
allByteArrays[i] = bigIntegers[i].ToByteArray();
Upvotes: 2