如何使用PowerShell将带有地址和ASCII值的HEX字符串转换为二进制文件?

发布于 2025-01-21 15:44:41 字数 4987 浏览 0 评论 0 原文

我有一个带有此内容的文件:

00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
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

它返回此输出:

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   ................

任何人都可以给出想法。多谢。

I have a file with this content:

00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
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   ................

I would like to create .bin file with with content.
I tried this way:

$bytes = [byte]0xFF 
Set-Content -Path .\content.bin -Value $bytes -Encoding Byte

It created the bin file but when I try to check the hex with this way:

Format-Hex .\content.bin

It return this output:

00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

00000000   FF 

My expectation result supposed to be like this:

    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00   ................
    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   ................

Anyone can give idea please. Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居人 2025-01-28 15:44:41

通过您的评论,我想将十六进制转换为.bin文件。

有许多应用程序可以执行二进制文件的六角形。 PowerShell具有
格式-HEX以字节计数标头行开始

00 01 02 03 04 04 06 06 07 08 09 0A 0A 0B 0C 0C 0D 0E 0E 0F

然后是一个空行,之后的HEX数据。
它还在实际数据之前添加了一个字节计数器左边。

您的示例没有这样做,而是在每八个字节之间添加连字符,因此显然不是使用格式 - hex创建它。

无论如何,这就是您可以从两种类型的六型输出中重新创建二进制文件的方法:

$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()

Going by your comment I want to convert the hex to .bin file.

There are many applications that can do a hexdump of a binary file. PowerShell has the Format-Hex cmdlet for that, but it doesn't create output as you show in your question.
Format-Hex starts with a byte count header line

00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

then an empty line and after that the hex data.
It also adds a byte counter left margin before the actual data.

Your example doesnt do that, but instead adds a hyphen in between every eight bytes, so it is obviously not created using Format-Hex.

Anyway, here's how you can recreate a binary file from both types of hexdump output:

$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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文