Search code examples
powershellhexlarge-datalargenumber

Powershell large hex range


I am trying to make a range of hex from a000000000-afffffffff, but the values are too large for Int32. Getting error Cannot convert value "687194767360" to type "System.Int32". Error: "Value was either too large or too small for an Int32."

Here is the code (0xa000000000..0xafffffffff|% ToString X10).ToLower()


Solution

  • The endpoints of .., the range operator, must fit into [int] values, which your numbers exceed.

    • Additionally, since you're trying to collect the output of the streaming 0xa000000000..0xafffffffff| % ToString X10 pipeline command in memory in full, by enclosing it in (...) and calling method .ToString() on it, the collected outputs would have to fit into an array, and the count of elements in your range exceed the max. capacity of a .NET array too.

    Similarly, the [Linq.Enumerable]::Range() method unfortunately also only accepts [int] endpoints.

    However, you can implement a (custom) streaming solution, where PowerShell creates and emits each formatted number (string) to the pipeline one by one:

    $num = 0xa000000000
    while ($num -le 0xafffffffff) { ($num++).ToString('x10') }
    

    Note:

    • While this works, it will be slow.

    • You won't be able to capture the result as a whole in a collection in memory.

      • If you were to stream the output to a file unfiltered, you'd end up with a file 704 GB(!) / 768 GB(!) in size on Unix-like platforms / Windows.
      • In-memory, partitioning the output into chunk(s) small enough to fit into array(s) is the only option.