'Reversing byte order in PowerShell

I want to reverse the order of bytes in this Array

$cfg = ($cfg | ForEach-Object ToString X2)

I have tried

$cfg = [Array]::Reverse($cfg)

But after

Write-Output $cfg

There was no generated output. How can I approach this problem?



Solution 1:[1]

If you're trying to convert a byte array to integer in LittleEndian order or "backwards"... (based on Powershell Byte array to INT)

[bitconverter]::ToInt16   # without parentheses to get declaration

OverloadDefinitions
-------------------
static int16 ToInt16(byte[] value, int startIndex)


$bytes = [byte[]](0xde,0x07)
$startIndex = 0

[bitconverter]::ToInt16($bytes,$startIndex)
2014

2014 | % tostring x4
07de                     # bytes are backwards

[bitconverter]::IsLittleEndian   # Windows, OSX, Linux...
True

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1