Given
[byte[]] $byteArray = '0x33,0x32,0xff' -split ','
I would like to report this in the log as 33, 32, ff
since this is what the registry will show, and that is what the log will be compared to. If I don't do any formatting it's shown in decimal; 51, 50, 255
.
I can use
[System.BitConverter]::ToString($byteArray) -replace '-', ', '
with the replace there to reformat from xx-xx-xx
to xx, xx, xx
but I wonder if there is a more direct approach? PS 5.1 if that makes a difference.
I wonder if there is a more direct approach?
[System.BitConverter]::ToString($byteArray) -replace '-', ', '
as shown in your question, is as direct an approach as is possible - and probably the best-performing one.
Using PowerShell features only, the next best solution (favoring performance over concision) is the following:
$byteArray.ForEach({ $_.ToString('x2') }) -join ', '
(Using %
, the built-in alias of ForEach-Object
, a more concise solution is possible, but it'll perform worse:
($byteArray | % ToString x2) -join ', ')
)