Summary
I'm trying to convert a 32-bit BitArray
into an integer using PowerShell version 7. I've tried the C# way of handling it, but it doesn't work.
Here's my code.
$high = [System.Collections.BitArray]::new(@(0x93e0))
$low = [System.Collections.BitArray]::new(@(0x0004))
$low += $high.LeftShift(16)
# Create an integer array with a length of 1
$result = [int[]]::new(1)
# Copy the BitArray to the integer at index 0
$low.CopyTo($result), 0)
Actual Result
An exception is thrown.
MethodInvocationException: Exception calling "CopyTo" with "2" argument(s): "Destination array was not long enough. Check the destination index, length, and the array's lower bounds. (Parameter 'destinationArray')"
Expected Result
$result
variable is populated with the value of the BitArray
represented as an integer.
The issue is that I was adding two
BitArray
instances together, which caused$low
to become a 64-bitBitArray
.The correct solution is as follows.