Home > other >  How to convert Hex string with address and ASCII values to binary file using Powershell?
How to convert Hex string with address and ASCII values to binary file using Powershell?

Time:04-15

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.

CodePudding user response:

Your code works as expected. You are writing the single byte 0xFF into a file, which Format-Hex correctly shows.

If you want to write multiple bytes, pass an array to Set-Content:

[byte[]] $bytes = 0xFF, 0x00, 0x7F, 0xAB, 0x12

# For PS 7 , replace "-Encoding Byte" by "-AsByteStream"
Set-Content -Path .\content.bin -Value $bytes -Encoding Byte

Format-Hex .\content.bin

Output:

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

00000000   FF 00 7F AB 12                                   ..«.

As your data is relatively simple, you don't have to specify each byte individually though. Instead you can generate it like this:

# Create an array of 440 bytes, filled with zeros
[byte[]] $bytes = ,0 * 440

# Append a sequence of bytes
$bytes  = 0x2D, 0x13, 0x2D, 0x13

# Append an array of 68 bytes, filled with zeros
$bytes  = ,0 * 68

Alternatively could do it using this one-liner:

[byte[]] $bytes = (,0 * 440)   (0x2D, 0x13, 0x2D, 0x13)   (,0 * 68)

See this answer for more details about the syntax.

Output:

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

00000000   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000010   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000020   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000030   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000040   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000050   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000060   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000070   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000080   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000090   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000000A0   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000000B0   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000000C0   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000000D0   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000000E0   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000000F0   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000100   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000110   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000120   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000130   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000140   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000150   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000160   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000170   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000180   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000190   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000001A0   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000001B0   00 00 00 00 00 00 00 00 2D 13 2D 13 00 00 00 00  ........-.-.....
000001C0   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000001D0   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000001E0   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000001F0   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................

CodePudding user response:

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()
  • Related