使用批处理文件将二进制文件转换为十六进制表示 [英] converting a binary file to HEX representation using batch file
问题描述
我需要将 DLL 文件转换为 HEX 表示形式,以将其用作字符串的一部分来创建这样的 sql server 程序集
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
<代码>创建程序集 [AssemblyNameHere]
FROM 0x4D5A90000300000004000000FFFF000......继续二进制数据
WITH PERMISSION_SET = EXTERNAL_ACCESS
出于多种原因,我需要将它放在批处理文件中,但似乎 FOR 语句仅适用于文本文件.
I need this to be in a batch file for many reasons, but it seems that FOR statement only suitable for text files.
推荐答案
用纯批处理创建十六进制输出不是一个好主意.
It is not a very good idea to create a hex output with pure batch.
但您可以使用 vbscript 或 FC.exe 可以工作的简单任务.
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!
)
)
首先我创建一个长度(几乎)相同的文件,然后我将它们与二进制模式 (/B) 的 FC 进行比较,扫描输出,如果检测到缺少行,则用 hexVal 填充它们x10 字符串(在本例中为 0x41='A').
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').
这篇关于使用批处理文件将二进制文件转换为十六进制表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!