如何在Windows 7中本机查看文件的二进制内容?(可能吗。)


35

我有一个大于500MB的文件,这引起了一些问题。

我相信问题出在使用的行尾(EOL)约定中。我想以未解释的原始格式(1)查看该文件,以确认该文件的EOL约定。

如何使用Windows 7内置的工具查看文件的“二进制”?我希望避免下载任何其他内容。

(1)我和我的同事在文本编辑器中打开了文件,它们显示的行与预期的一样。但是,两个文本编辑器都将打开具有不同EOL约定的文件并自动解释它们。(TextEdit和Emacs 24.2。对于Emacs,我head -c4096在linux框上创建了仅具有前4K字节的第二个文件,并从Windows框打开了该文件。

我尝试在Emacs中使用hexl模式,但是当我进入hexl模式并返回到文本模式时,缓冲区的内容已更改,在每行的末尾添加了一个可见的^ M,所以我没有现在相信这一点。

我认为问题可能出在所使用的行字符的末尾。我和我的同事所尝试的编辑器(1)自动识别行结束符并向我们显示行。根据其他证据,我认为EOL约定仅是回车。(2)只退货。

要了解文件中实际包含的内容,我想查看文件的二进制内容,或者文件的至少几千个字节,最好使用十六进制,尽管我可以使用十进制或八进制。只是一个零将很难看。

更新

除了一个建议DEBUG,以下所有答案在某种程度上都起作用。我已对所有这些都给予了支持。我的问题形式不正确。在测试每个建议的解决方案时,我发现我确实希望并排查看十六进制和文本内容,并且希望它成为当我将光标悬停在某个内容(字节值或文本字符)上的内容时,另一面将突出显示。

当Emacs十六进制模式开始“正确”工作时,我实际上解决了我的问题。因此,我最终没有使用任何这些答案,仅测试了它们(真的应该调查奇怪的Emacs行为并提交错误报告)。


Cygwin下可能有某种工具,但这需要安装Cygwin。或者,例如,如果您的包装盒中装有Java,则用Java编写十六进制转储程序将是一个相当简单的任务。
Daniel R Hicks 2012年

Answers:


11

您需要一个“十六进制编辑器”。我已经使用“ Hex Editor Neo”多年了,它非常好。它有免费和收费版本。(而且我敢肯定还有其他类似的工具可用。)


4
我曾问过如何在Windows 7之外没有其他东西,因为我不喜欢添加其他程序,因为1)许多安装方式都是我所不允许的。2)有些看起来很狡猾。话虽如此,Hex编辑器Neo似乎是一个不错的建议。+1
香农遣散费

1
zblist.com是一个独立程序,无需安装或需要任何特殊权限,并具有Alt-H或十六进制模式
sgmoore'Aug

满足了我的所有要求,包括陈述的,遗漏的,未陈述的。十六进制编辑器Neo也很快,并且已添加到我的工具包中。
Shannon Severance 2012年

1.其他编辑器不是本机的。2.文件管理器很适合此目的,周围有很多可移植的文件管理器。3. Type命令可以本地显示内容,并具有有用的过滤器(如逐页显示)。
忘了

37

如果您具有Powershell 5.0或更高版本,则可以使用powershell内置功能Format-Hex

PS:21 C:\Temp >Format-Hex application.exe

            0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F

00000000   42 4D 5E 00 00 00 00 00 00 00 36 00 00 00 28 00  BM^.......6...(. 
00000010   00 00 0A 00 00 00 01 00 00 00 01 00 20 00 00 00  ............ ... 
00000020   00 00 00 00 00 00 C4 0E 00 00 C4 0E 00 00 00 00  ......Ä...Ä..... 
00000030   00 00 00 00 00 00 B7 59 71 FF B7 59 71 FF B7 59  ......•Yq.•Yq.•Y 
00000040   71 FF B7 59 71 FF B7 59 71 FF B7 59 71 FF B7 59  q.•Yq.•Yq.•Yq.•Y 
00000050   71 FF B7 59 71 FF B7 59 71 FF B7 59 71 FF        q.•Yq.•Yq.•Yq.

4
坦率地说,这不是最佳答案,对此我感到很惊讶。这是在Windows中使用内置工具进行操作的正确方法。如果要将输出写入文件,可以使用> Format-Hex application.exe> out.txt
techdude

这看起来不错,但是Format-Hex在我的PowerShell中不可用;我刚刚收到“无法识别”的错误
Kidburla

据JamieSee称,它显然是在Powershell 5.0之前才添加的。
techdude

26

内置,快速又脏乱:start powershell,执行:

gc -encoding byte -TotalCount 100 "your_file_path" |% {write-host ("{0:x}" -f $_) -noNewline " "}; write-host   

TotalCount是您要从文件读取的字节数。

Google的“ powershell hexdump”获得了更多完善/可行的版本。

如果您具有Windows Resource Kit工具(不是完全内置的,而是关闭的),则还可以使用一个名为的cmd行实用程序list.exe。这是一个十六进制模式的小型编辑器。专为处理大文件而设计:

列表文本文件工具(列表)是一种命令行工具,用于显示和搜索一个或多个文本文件。与其他文本显示工具不同,列表在打开时不会将整个文件读入内存。它允许用户以十六进制格式编辑文本文件。

列表对于远程显示文本或日志文件以及在管理员担心系统性能下降的服务器上使用很有用。


1
到目前为止,此解决方案是我所要求的最接近的解决方案。
香农遣散费

1
不错,简单,已经安装。我将格式更改为write-host(“ {0:X2}”强制0x0A出现为“ 0A”而不是“ A”,2表示2位大写字母,因为这就是我喜欢的方式
Adam Straughan

1
List.exe非常完美-help list.exe /?命令提供的信息不多,但是一旦进入编辑器,只需点击?即可查看命令。H打开十六进制编辑器,并F1切换十六进制的显示方式
Coruscate5

7

这在XP之后的所有版本上也适用:

certutil -encodehex MyProgram.exe MyProgram.txt

XP需要从此处获取Windows Server 2003管理工具包:

https://www.microsoft.com/zh-cn/download/details.aspx?id=16770


Windows上最便携式和向后兼容的解决方案,甚至可以从Windows批处理脚本中使用,感到惊讶的是,为什么这仍然不在所有答案的首位
Andry

6

将文件复制到带有.COM扩展名的名称,该扩展名的基本名称不得超过8个字符。跑

DEBUG your_filename

它将给出一个“ -”提示。类型

DEnter

反复d在时间isplay文件128个字节。类型

D 地址 Enter

显示从address开始的128个字节,该地址必须以十六进制键入,其中文件的开头是地址100。

D 地址1地址2 Enter

地址1地址2显示。类型

D 地址 LNUM Enter

显示从address开始的num个字节(长度)。  num也以十六进制输入。使用戒烟。Q

例如,

C:\Users\scott\Documents> debug thispost.com
-d
0BE4:0100  43 6F 70 79 20 74 68 65-20 66 69 6C 65 20 74 6F   Copy the file to
0BE4:0110  20 61 20 6E 61 6D 65 20-77 69 74 68 20 61 20 2E    a name with a .
0BE4:0120  43 4F 4D 20 65 78 74 65-6E 73 69 6F 6E 2C 20 77   COM extension, w
0BE4:0130  68 65 72 65 20 74 68 65-20 62 61 73 65 20 6E 61   here the base na
0BE4:0140  6D 65 20 69 73 20 6E 6F-20 6C 6F 6E 67 65 72 20   me is no longer
0BE4:0150  74 68 61 6E 20 65 69 67-68 74 20 63 68 61 72 61   than eight chara
0BE4:0160  63 74 65 72 73 2E 0D 0A-52 75 6E 20 44 45 42 55   cters...Run DEBU
0BE4:0170  47 20 2A 79 6F 75 72 5F-66 69 6C 65 6E 61 6D 65   G *your_filename
-d
0BE4:0180  2A 0D 0A 49 74 20 77 69-6C 6C 20 67 69 76 65 20   *..It will give
0BE4:0190  61 20 27 2D 27 20 70 72-6F 6D 70 74 2E 0D 0A 54   a '-' prompt...T
0BE4:01A0  79 70 65 20 44 20 45 6E-74 65 72 20 72 65 70 65   ype D Enter repe
0BE4:01B0  61 74 65 64 6C 79 20 74-6F 20 2A 2A 64 2A 2A 69   atedly to **d**i
0BE4:01C0  73 70 6C 61 79 20 74 68-65 20 66 69 6C 65 20 31   splay the file 1
0BE4:01D0  32 38 20 62 79 74 65 73-20 61 74 20 61 20 74 69   28 bytes at a ti
0BE4:01E0  6D 65 2E 0D 0A 54 79 70-65 20 44 20 5F 61 64 64   me...Type D _add
0BE4:01F0  72 65 73 73 5F 20 74 6F-20 64 69 73 70 6C 61 79   ress_ to display
-d 200 L16
0BE4:0200  20 31 32 38 20 62 79 74-65 73 20 73 74 61 72 74    128 bytes start
0BE4:0210  69 6E 67 20 61 74                                 ing at
-

3
不幸的是,如果文件大于约64KB(.COM的最大值),则无法使用。(它必须适合从偏移100h开始的分段。)
肯(Ken

1
C:\>attrib debug.exe /s。结果:File not found - debug.exe。无法找到官方的,不再支持调试的声明,但是从我在网络上看到的情况来看,似乎调试支持已经删除了一段时间。我从Microsoft找到了DebugDiag。(额外下载。)调试吗?也许它支持以十六进制格式查看文件?以.MSI文件形式提供。需要管理员密码才能安装。我不是一个
香农遣散费

我已经head -c4096 bigFileName > smallFileName在Linux上使用@Ken 来获取文件的前4 KB。行足够小,以至于我有4 KB的行就足够了
Shannon Severance 2012年

那么,为什么不在hexdump -CLinux上同时使用呢?

3
@Shannon调试是DOS的一部分,因此,如果您使用的是x64,则它不存在。
kinokijuf 2012年



2

这不是理想的方法,但是如果您真的不想下载任何内容,则可以尝试使用 fc / b(即二进制模式下的文件比较)将该文件与另一个完全不同的文件进行比较,它将显示十六进制每个字节的值都不同。您可能会得到一些在两个文件中相同的值,因此可能会从输出中跳过它们,但是您可以通过检查offset列中的缺失值来判断是否发生了这种情况。


不理想,但是我可以通过创建一个0x00字节的文件然后与之进行比较来做到这一点。作为一个我正在查看的文本文件,并且我对0a和0d感兴趣,因此在比较工作时大部分都是空文件。但是它并不能同时提供字符视图和十六进制视图,这使我很难找到想要的位置。(就像Scott回答中的debug一样,Emacs的hexl-mode一样。我没有要求并排查看,但这对于我实际使用hex dumps至关重要。)
Shannon Severance 2012年

2

您可以将以下PowerShell函数与Get-Content一起使用,以查看文件内容的十六进制转储,即 Get-Content -Encoding Byte 'MyFile.bin' | Format-HexDump。转储222 KB文件大约需要23秒,并且,如果需要,可以将输出重定向到文本文件,以使转储检查更加容易。

$encodingAutoCompleter = {
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
    $availableEncodings = ([System.Text.Encoding]::GetEncodings() | Select Name, CodePage, DisplayName) + @( [PSCustomObject] @{ CodePage = '20127'; Name = 'ascii'; DisplayName = 'US-ASCII' }, [PSCustomObject] @{ CodePage = '1200'; Name = 'unicode'; DisplayName = 'Unicode' } )
    $availableEncodings | ?{ $_.Name.StartsWith($wordToComplete) } | %{ New-Object System.Management.Automation.CompletionResult -ArgumentList $_.Name, $_.Name, 'ParameterValue', "$($_.DisplayName). Code Page $($_.CodePage)." }
}

function Format-BufferText([byte[]] $buffer, [System.Text.Encoding] $displayEncoding, [switch] $useControlPictures)
{
    $bufferChars = $displayEncoding.GetChars($buffer);
    $bufferText = (($bufferChars | %{ if ([char]::IsControl($_) -eq $true) { if ($useControlPictures -eq $false) { '.' } else { [char] ($_.ToInt16([cultureinfo]::InvariantCulture) + 0x2400) } } else { "$_" } }) -join "")

    $bufferText
}

<#
    .Synopsis
    Displays binary data as a hexadecimal dump.

    .Description
     Displays binary data as a hexadecimal dump. Options are available to suppress displaying text and to display control characters 
     as Unicode Control Pictures instead of dots.

    .Parameter Bytes
    The bytes to be displayed.

    .Parameter Encoding
    The name of the text encoding to use. The default is ascii.

    .Parameter NoTextDisplay
    If specified the text display sidebar will be suppressed; otherwise, the display text sidebar will be present.

    .Parameter UseControlPictures
    If specified control characters will be displayed as Unicode Control pictures; otherwise, dots are used to represent control 
    characters.

    .Example
    Format-HexDump -Encoding unicode $bytes

    .Example
    Get-Content -Encoding Byte 'MyFile.bin' | Format-HexDump -Encoding unicode

    .Example
    0..255 | Format-HexDump -NoTextDisplay
#>
function Format-HexDump
{
    [CmdletBinding()]
    param
    (
        [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [byte[]] $Bytes,
        [ValidateScript({ if (([System.Text.Encoding]::GetEncodings().Name + @('unicode', 'ascii')) -icontains $_) { return $true } else { Throw "Encoding must be one of the following: $([System.Text.Encoding]::GetEncodings().Name -join ', '), unicode, or ascii." } })]
        [Parameter(ValueFromPipeline = $false)]
        [string] $Encoding = "ASCII",
        [Parameter()]
        [switch] $NoTextDisplay,
        [Parameter()]
        [switch] $UseControlPictures
    )

    BEGIN
    {
        $displayEncoding = [System.Text.Encoding]::GetEncoding($Encoding)

        $counter = 0
        $hexRow = ""
        [byte[]] $buffer = @()
    }

    PROCESS
    {
        foreach ($byte in $Bytes)
        {
            $buffer += $byte
            $hexValue = $byte.ToString("X2")

            if ($counter % 16 -eq 0)
            {
                $buffer = @($byte)
                $hexRow = "$($counter.ToString("X8")): $($hexValue) "
            }
            elseif ($counter % 16 -eq 15)
            {
                if ($NoTextDisplay -eq $true)
                {
                    $hexRow += "$($hexValue)"
                    $hexRow
                }
                else
                {
                    $bufferText = Format-BufferText $buffer $displayEncoding $UseControlPictures
                    $hexRow += "$($hexValue)   $($bufferText)"
                    $hexRow
                }
            }
            else
            {
                $hexRow += "$($hexValue) "
            }

            $counter++
        }
    }

    END
    {
        $counter--

        if ($counter % 16 -ne 15)
        {
            $hexRow += " " * (((16 - $counter % 16) * 3) - 1)

            if ($NoTextDisplay -eq $false)
            {
                $bufferText = Format-BufferText $buffer $displayEncoding $UseControlPictures
                $hexRow += "$($bufferText)"
            }

            $hexRow
        }
    }
}

Register-ArgumentCompleter -CommandName Format-HexDump -ParameterName Encoding -ScriptBlock $encodingAutoCompleter

输出看起来像这样:

00000000: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F   ................
00000010: 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F   ................
00000020: 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F    !"#$%&'()*+,-./
00000030: 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F   0123456789:;<=>?
00000040: 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F   @ABCDEFGHIJKLMNO
00000050: 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F   PQRSTUVWXYZ[\]^_
00000060: 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F   `abcdefghijklmno
00000070: 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F   pqrstuvwxyz{|}~.
00000080: 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F   ????????????????
00000090: 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F   ????????????????
000000A0: A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF   ????????????????
000000B0: B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF   ????????????????
000000C0: C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF   ????????????????
000000D0: D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF   ????????????????
000000E0: E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF   ????????????????
000000F0: F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF   ????????????????

或仅使用Format-Hex文件名
techdude

@techdude Format-Hex在所有版本的PowerShell中均不可用。它在PowerShell 4和更低版本中不存在。我在Format-Hex存在之前就编写了此代码。
JamieSee '18

如何将其恢复为二进制文件?
Zimba


0

我知道您正在使用Emacs,但Vim用户可以使用xxd实用程序:

xxd -s <start_offset> -l <length_offest> <file>

Usage:
       xxd.exe [options] [infile [outfile]]
    or
       xxd.exe -r [-s [-]offset] [-c cols] [-ps] [infile [outfile]]
Options:
    -a          toggle autoskip: A single '*' replaces nul-lines. Default off.
    -b          binary digit dump (incompatible with -ps,-i,-r). Default hex.
    -c cols     format <cols> octets per line. Default 16 (-i: 12, -ps: 30).
    -E          show characters in EBCDIC. Default ASCII.
    -g          number of octets per group in normal output. Default 2.
    -h          print this summary.
    -i          output in C include file style.
    -l len      stop after <len> octets.
    -ps         output in postscript plain hexdump style.
    -r          reverse operation: convert (or patch) hexdump into binary.
    -r -s off   revert with <off> added to file positions found in hexdump.
    -s [+][-]seek  start at <seek> bytes abs. (or +: rel.) infile offset.
    -u          use upper case hex letters.
    -v          show version: "xxd V1.10 27oct98 by Juergen Weigert (Win32)".
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.