使用 Powershell 读取/解析二进制文件 [英] Read/Parse Binary files with Powershell

查看:47
本文介绍了使用 Powershell 读取/解析二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析一个二进制文件,我需要一些关于去哪里的帮助.我一直在网上寻找解析二进制文件"、读取二进制文件"、读取二进制文件中的文本"等,但我没有任何运气.

I'm trying to parse a binary file, and I need some help on where to go. I've looking online for "parsing binary files", "reading binary files", "reading text inside binaries", etc. and I haven't had any luck.

例如,我如何从这个二进制文件中读取这个文本?任何帮助将非常感激.我正在使用 powershell.

For example, how would I read this text out of this binary file? Any help would be MUCH appreciated. I am using powershell.

推荐答案

您似乎有一个二进制文件,其文本位于固定或可推断的位置.Get-Content 可能会对您有所帮助,但...它会尝试将整个文件解析为字符串数组,从而创建一个垃圾"数组.此外,您不会知道特定字符绳"是从哪个文件位置开始的.

It seems that you have a binary file with text on a fixed or otherwise deducible position. Get-Content might help you but... It'll try to parse the entire file to an array of strings and thus creating an array of "garbage". Also, you wouldn't know from what file position a particular "rope of characters" was.

您可以尝试 .NET 类 File 阅读和 编码 进行解码.每次调用只需一行:

You can try .NET classes File to read and Encoding to decode. It's just a line for each call:

# Read the entire file to an array of bytes.
$bytes = [System.IO.File]::ReadAllBytes("path_to_the_file")
# Decode first 12 bytes to a text assuming ASCII encoding.
$text = [System.Text.Encoding]::ASCII.GetString($bytes, 0, 12)

在您的实际情况中,您可能会在循环中遍历字节数组,查找特定字符串序列的开始和结束,并使用这些索引指定要从中提取文本的字节范围 GetString.

In your real case you'd probably go through the array of bytes in a loop finding the start and end of a particular string sequence and using those indices to specify the range of bytes you want to extract the text from by the GetString.

我提到的 .NET 方法在 .NET Framework 2.0 或更高版本中可用.如果您安装了 PowerShell 2.0,那么您已经拥有它.

The .NET methods I mentioned are available in .NET Framework 2.0 or higher. If you installed PowerShell 2.0 you already have it.

这篇关于使用 Powershell 读取/解析二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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