如何将文件中的字节与值的文字数组进行比较?
$path = "C:\BinaryFile.bin"
$bytes = Get-Content -LiteralPath $path -Encoding byte -TotalCount 4 -ReadCount 4
if ($bytes -eq [0 1 2 3]) { # How to compare to expected bytes?
Write-Output "OK: $bytes"
}
最佳答案
也许比较对象。没有输出意味着相等。 Syncwindow 0 以获得准确的顺序。
$path = 'file'
[byte[]](0..3) | set-content $path -Encoding Byte
$bytes = Get-Content $path -Encoding byte -TotalCount 4
if (! (compare-object $bytes 0,1,2,3 -syncwindow 0)) {
"OK: $bytes"
}
或者做一个字符串比较:
if ("0 1 2 3" -eq $bytes) {
"OK: $bytes"
}
关于powershell - 检查文件的前 n 个字节是否在 Powershell 中是特定的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70927857/