converting a binary file to HEX representation using batch file

后端 未结 2 1065
情深已故
情深已故 2020-12-18 12:53

I need to convert a DLL file to HEX representation to use it as a part of string to create a sql server assembly like this

CREATE ASSEMBLY [AssemblyNameHere]<

相关标签:
2条回答
  • 2020-12-18 12:55

    It is not a very good idea to create a hex output with pure batch.

    But you could use vbscript or for simple tasks FC.exe could work.

    @echo off
    SETLOCAL EnableDelayedExpansion
    set filesize=%~z1
    set "hexVal=41"
    set "x10=AAAAAAAAAA"
    
    set /a chunks=1+filesize / 10
    
    del dummy.txt 2>nul > nul
    for /L %%n in (0,1,%chunks%) DO (
      <nul >> dummy.txt set /p ".=%x10%"
    )
    
    set /a expectedNum=0
    for /F "eol=F usebackq tokens=1,2 skip=1 delims=:[] " %%A in (`fc /b "%~dpf1" dummy.txt`) DO (
        set /a num=0x%%A && (
                set /a numDec=num-1
            set "hex=%%B"
    
            for /L %%n in (!expectedNum!=,=1 !numDec!) DO (
                echo %hexVal%
            )
            set /a expectedNum=num+1
            echo !hex!
        )
    )
    

    First I create a file with (nearly) the same length, and then I compare them with FC in binary mode (/B), the output is scanned and if a line missings are detected, they are filled with the hexVal of the x10 string (in this case 0x41='A').

    0 讨论(0)
  • 2020-12-18 13:18

    the easiest way is with CERTUTIL command:

    certutil -encodehex c:\myfile.dll myfile.hex
    
    0 讨论(0)
提交回复
热议问题