Write HEX values to file in Windows batch

前端 未结 2 549
南笙
南笙 2021-01-01 00:53

In Linux we can do

echo -n -e \'\\x66\\x6f\\x6f\' > test.txt

to write HEX values to a file.

How can this be done simply in Windo

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-01 01:46

    To write "HEX values to file":

    set a=.
    for /l %a in (0,1,255) do (
    set /a dec=%a >nul
    call cmd /c exit /b !dec!
    set hex=!=exitcode!
    set a=!a! !hex:~-2!
    )
    echo %a:~2% > file.txt
    

    To create a binary file from batch with all the values, see here:
    https://www.dostips.com/forum/viewtopic.php?f=3&t=5326

    certutil can do both: create a hex file from a binary, and create a binary file from a hex coded file:

    certutil -f -decodeHex hex.txt out.bin >nul
    certutil -f -encodeHex out.bin out.txt >nul
    

    To write a binary file with given (decoded) hex value bytes, from CMD:

    set data="66 6F 6F"
    echo Dim File : Set File = CreateObject("Scripting.FileSystemObject").CreateTextFile("test.txt") > tmp.vbs
    echo data = %data% >> tmp.vbs
    echo data = Split (data) >> tmp.vbs
    echo for each x in data >> tmp.vbs
    echo File.write chr("&H" ^& x) >> tmp.vbs
    echo next >> tmp.vbs
    echo File.Close >> tmp.vbs
    cscript //nologo tmp.vbs
    

    If you're just after writing printable (from 0x20 through 0x7E) characters, then !=ExitCodeASCII! is a good/faster (batch) solution.

提交回复
热议问题