如何使用 Powershell 将带有地址和 ASCII 值的十六进制字符串转换为二进制文件?
How to convert Hex string with address and ASCII values to binary file using Powershell?
我有一个包含以下内容的文件:
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 2D 13 2D 13 00 00 00 00 ........-.-.....
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
我想创建包含内容的 .bin 文件。
我这样试过:
$bytes = [byte]0xFF
Set-Content -Path .\content.bin -Value $bytes -Encoding Byte
它创建了 bin 文件,但是当我尝试用这种方式检查十六进制时:
Format-Hex .\content.bin
它return这个输出:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00000000 FF
我的预期结果应该是这样的:
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 2D 13 2D 13 00 00 00 00 ........-.-.....
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
请大家出出主意。非常感谢。
根据您的评论我想将十六进制转换为 .bin 文件。
有许多应用程序可以对二进制文件进行十六进制转储。 PowerShell 有 Format-Hex cmdlet,但它不会像您在问题中显示的那样创建输出。
Format-Hex 以字节数开头 header 行
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
然后是一个空行,然后是十六进制数据。
它还在实际数据之前添加了一个字节计数器左边距。
您的示例没有这样做,而是在每八个字节之间添加一个连字符,因此显然不是使用 Format-Hex.
创建的
无论如何,下面是从两种类型的 hexdump 输出中重新创建二进制文件的方法:
$inputFile = 'D:\Test\yourHexdump.txt'
$outputFile = 'D:\Test\yourBinaryFile.bin' # use absolute full path here
# if the output file already exists, delete it
if (Test-Path -Path $outputFile -PathType Leaf) { Remove-Item -Path $outputFile -Force }
# create a FileStream object to write the converted bytes to file
$stream = [System.IO.FileStream]::new($outputFile, [System.IO.FileMode]::Append, [IO.FileAccess]::Write)
# now loop through the file line-by-line and parse out the bytes
switch -Regex -File $inputFile {
# if this is from Format-Hex output, ignore the 1-16 byte header and empty line(s)
'^\s+00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|^\s*$' {}
default {
# if this is from Format-Hex output, remove the byte counters in the left margin
$line = if ($_ -match '^[0-9A-f]{8}\s') { $_.Substring(8) } else { $_ }
# convert the hex values into an array of bytes
[byte[]]$bytes = (($line.TrimStart() -split '\s{2,}')[0].Trim() -replace '-' -split '\s+' -ne '' -replace '^', '0X')
# write to file
$stream.Write($bytes, 0, $bytes.Count)
}
}
# dispose of the stream
$stream.Flush() # probably Dispose() also flushes..
$stream.Dispose()
我有一个包含以下内容的文件:
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 2D 13 2D 13 00 00 00 00 ........-.-.....
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
我想创建包含内容的 .bin 文件。 我这样试过:
$bytes = [byte]0xFF
Set-Content -Path .\content.bin -Value $bytes -Encoding Byte
它创建了 bin 文件,但是当我尝试用这种方式检查十六进制时:
Format-Hex .\content.bin
它return这个输出:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00000000 FF
我的预期结果应该是这样的:
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 2D 13 2D 13 00 00 00 00 ........-.-.....
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
请大家出出主意。非常感谢。
根据您的评论我想将十六进制转换为 .bin 文件。
有许多应用程序可以对二进制文件进行十六进制转储。 PowerShell 有 Format-Hex cmdlet,但它不会像您在问题中显示的那样创建输出。
Format-Hex 以字节数开头 header 行
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
然后是一个空行,然后是十六进制数据。 它还在实际数据之前添加了一个字节计数器左边距。
您的示例没有这样做,而是在每八个字节之间添加一个连字符,因此显然不是使用 Format-Hex.
创建的无论如何,下面是从两种类型的 hexdump 输出中重新创建二进制文件的方法:
$inputFile = 'D:\Test\yourHexdump.txt'
$outputFile = 'D:\Test\yourBinaryFile.bin' # use absolute full path here
# if the output file already exists, delete it
if (Test-Path -Path $outputFile -PathType Leaf) { Remove-Item -Path $outputFile -Force }
# create a FileStream object to write the converted bytes to file
$stream = [System.IO.FileStream]::new($outputFile, [System.IO.FileMode]::Append, [IO.FileAccess]::Write)
# now loop through the file line-by-line and parse out the bytes
switch -Regex -File $inputFile {
# if this is from Format-Hex output, ignore the 1-16 byte header and empty line(s)
'^\s+00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|^\s*$' {}
default {
# if this is from Format-Hex output, remove the byte counters in the left margin
$line = if ($_ -match '^[0-9A-f]{8}\s') { $_.Substring(8) } else { $_ }
# convert the hex values into an array of bytes
[byte[]]$bytes = (($line.TrimStart() -split '\s{2,}')[0].Trim() -replace '-' -split '\s+' -ne '' -replace '^', '0X')
# write to file
$stream.Write($bytes, 0, $bytes.Count)
}
}
# dispose of the stream
$stream.Flush() # probably Dispose() also flushes..
$stream.Dispose()