Read/Parse Binary files with Powershell

后端 未结 3 1164
一整个雨季
一整个雨季 2021-01-01 11:41

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 b

相关标签:
3条回答
  • 2021-01-01 12:17

    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.

    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)
    

    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.

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

    0 讨论(0)
  • 2021-01-01 12:18

    If you're just looking for strings, check out the strings.exe utility from SysInternals.

    0 讨论(0)
  • 2021-01-01 12:21

    You can read in the file via Get-Content -Encoding byte . I'm not sure how to parse it though.

    0 讨论(0)
提交回复
热议问题