是否可以使用Windows命令行编辑二进制文件? [英] Is it possible to edit a binary file using Windows command line?

查看:199
本文介绍了是否可以使用Windows命令行编辑二进制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows中是否可以通过命令行编辑二进制文件?即可以写入批处理文件的方式?

Is there a way in Windows to edit a binary file, from the command line? i.e. a way that could be written into a batch file?

我希望能够在现有文件中的已知位置编辑单个字节.

I want to be able to edit a single byte, at a known position, in an existing file.

这个现有问题[1]已解决,但这是Linux解决方案.我正在寻找适用于Windows的类似软件.

This existing question[1] is solved, but that's a Linux solution. I'm looking for something similar for Windows.

背景

从Steam下载时,GTA 1中存在一个错误,该错误导致保存游戏数据文件在退出时被破坏.结果,游戏可以在第一次玩的很好,但随后崩溃.事实证明,可以通过将文件中的第5个字节(即地址0x04的字节)从x00更改为x06 [2]来解决此问题.

There's a bug in GTA 1 when downloaded from Steam whereby the save-game data file gets corrupted on exit. As a result, the game can be played fine the first time but subsequently crashes. It turns out this can be fixed by changing the 5th byte in the file (i.e. the byte at address 0x04) from x00 to x06[2].

我可以在Python中轻松完成此操作,例如:

I can do this in Python easily, e.g.:

with open("PLAYER_A.DAT", "rb") as f:
    bytes = f.read()
bytes = bytes[:4] + '\x06' + bytes[5:]
with open("PLAYER_A.DAT", "wb") as g:
    for b in bytes: g.write(b)

理想情况下,尽管我宁愿在执行以下操作的批处理作业中执行此操作:

Ideally though I'd rather do this in a batch job that does the following:

  • 修复数据文件
  • 启动GTA

我可以(使用Python)制作对我有用的东西,但是这对随机选择其他没有Python的人没有帮助(是的,我知道很容易安装和安装,但仍然可以).同样,有一个免费软件声称可以做到这一点,但是我不想在我的PC上运行随机的.exe,而且我也不认为其他人也应该这样做.因此,我想提供一个批处理文件,人们可以检查它,并且-如果他们对它的功能感到满意-可以自己运行.

I could make something that works for me (using Python), but that wouldn't help random other people who don't have Python (yes I know it's easy to get & install, but still). Similarly, there is a freeware available that claims to do just this, but I don't want to run a random .exe on my PC, and I don't think anyone else should either. For that reason, I'd like to present a batch file, that people can inspect, and - if they're happy with what it does - run for themselves.

感谢您的帮助!

[1] CLI:在地址处写入字节(从命令行十六进制编辑/修改二进制文件)

[2] http://forums.steampowered.com/forums/showthread.php?t = 1597746

[edit]修复了Python脚本,因为我发现它无法按原样工作(file.read()返回一个不可变的对象,因此您不能只更新其中一个值).

[edit] Fixed up the Python script, as I found it didn't work as-is (file.read() returns an immutable object, so you can't just update one of the values).

推荐答案

我认为PowerShell是完成此任务的理想工具.它适用于XP或更高版本,并且自Windows 7起自动发货:

I think PowerShell is a perfect tool for this task. It's available for XP or higher and is automatically shipped since Windows 7:

只需创建一个具有以下内容的 *.ps1 文件:

Just create a *.ps1 file with this content:

$bytes = [System.IO.File]::ReadAllBytes("PLAYER_A.DAT");
$bytes[4] = 0x06;

[System.IO.File]::WriteAllBytes("PLAYER_A.DAT", $bytes);
& "C:\Path-To-GTA1-Exe-File.exe"

请注意,必须启用未签名的PowerShell脚本:

Note that one has to enable unsigned PowerShell scripts:

  1. 以管理员身份启动PowerShell

  1. Start PowerShell as an administrator

运行以下命令: Set-ExecutionPolicy RemoteSigned


您也可以使用VBScript,但是该脚本会更长一些,因为它不是为读取二进制文件而设计的(必须使用 ADODB.Stream 对象).

这是辅助函数的汇编: http://www.motobit.com/tips/detpg_read-write-binary-files/

Here is a compilation of helper functions: http://www.motobit.com/tips/detpg_read-write-binary-files/

这篇关于是否可以使用Windows命令行编辑二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆