在 Windows 批处理中将 HEX 值写入文件 [英] Write HEX values to file in Windows batch

查看:23
本文介绍了在 Windows 批处理中将 HEX 值写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Linux 中我们可以做到

In Linux we can do

echo -n -e 'x66x6fx6f' > test.txt

将 HEX 值写入文件.

to write HEX values to a file.

如何在 Windows 批处理中简单地完成?

How can this be done simply in Windows batch?

推荐答案

我假设您希望能够在 x00 到 xFF 范围内写入所有可能的二进制字节.不幸的是,纯批处理并没有提供一个简单的机制来做到这一点.

I am assuming you want the ability to write all possible binary bytes in the range x00 through xFF. Unfortunately, pure batch does not provide a simple mechanism to do this.

但有很多选择并不太难.

But there are a number of options that are not too difficult.

!=ExitCodeASCII! 动态变量报告最近运行的外部命令的返回码的 ASCII 值.但仅限于从 x20 到 x7E 的 ASCII 码.

The !=ExitCodeASCII! dynamic variable reports the ASCII value of the return code of the most recently run external command. But it is limited to ASCII codes from x20 through x7E.

我使用延迟扩展,这样我就不用担心毒字符了.

I use delayed expansion so that I don't have to worry about poison characters.

返回特定错误代码的最简单机制是使用 cmd/c exit N,其中 N 必须是十进制值.如果你想传入一个十六进制值,那么这个值必须先转换成十进制.

The simplest mechanism to return a specific error code is to use cmd /c exit N, where N must be a decimal value. If you want to pass in a hex value, then the value must first be converted to decimal.

Windows 批处理使用 0xNN 表示法指定十六进制值.正如其他人所指出的,您可以使用 set/a val=0x66 进行转换.另一种选择是使用 for/l %%N in (0x66 1 0x66) do ...,优点是您不需要定义中间环境变量来保存该值.p>

Windows batch uses 0xNN notation to specify a hex value. As others have noted, you can use set /a val=0x66 to do the conversion. Another option is to use for /l %%N in (0x66 1 0x66) do ..., the advantage being you don't need to define an intermediate environment variable to hold the value.

@echo off
setlocal enableDelayedExpansion
set "str="
for %%H in (0x66 0x6f 0x6f) do (
  for /l %%N in (%%H 1 %%H) do cmd /c exit %%N
  set "str=!str!!=ExitCodeASCII!"
)
>test.txt echo(!str!

优点:

  • 纯批次

缺点:

  • 必须一次构建一个字符的字符串
  • 范围限制为 0x20 - 0x7E

FORFILES 命令支持 0xNN 语法,因此它可以生成大多数字符.但是字符串必须经过CMD/C,所以不能用来生成x00、x0A、x0D.(我没有测试过,但我相信所有其他值都有效,前提是适当引用或转义毒字符)

The FORFILES command supports 0xNN syntax, so it can generate most characters. But the string must pass through CMD /C, so it cannot be used to generate x00, x0A, or x0D. (I haven't tested, but I believe all other values work, provided poison characters are appropriately quoted or escaped)

@echo off
forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(0x660x6f0x6f"

优点:

  • 纯批次
  • 可以一次处理整个字符串

缺点:

  • 不支持原生 XP
  • 不支持 0x00、0x0A 或 0x0D
  • 转义/引用有毒字符可能很棘手

CERTUTIL 支持 -decodeHex 动词,可以读取十六进制值并直接写入文件

The CERTUTIL supports a -decodeHex verb that can read hex values and write directly to a file

@echo off
>temp.txt echo(66 6f 6f 0d 0a
certutil -f -decodehex temp.txt test.txt >nul
del temp.txt

优点:

  • 纯批次
  • 支持所有可能的字节码
  • 换行和回车的绝对控制
  • 可以一次性处理整个字符串(或文件!)
  • 快速

缺点:

  • 需要一个临时文件

在批处理脚本中嵌入和执行 JScript 非常容易.JScript 具有解释许多转义序列的本机能力,包括 xNN.但是,xNN 转义序列实际上映射到 Unicode 代码点,因此一些高位字节代码没有映射到正确的字符值.高位字节的结果可能会因您的机器默认字符集而异.

It is very easy to embed and execute JScript within a batch script. And JScript has native ability to interpret many escape sequences, including xNN. However, the xNN escape sequences actually map to Unicode code points, so some of the high order byte codes do not map to the correct character values. And the results for high order bytes can vary depending on your machines default character set.

下面我定义了一个 :jWrite 子例程,它可以编写带有嵌入转义序列的行.如果要编写不带换行符的字符串,只需将 JScript 代码中的 WriteLine 更改为 Write.

Below I define a :jWrite subroutine that can write lines with embedded escape sequences. Simply change the WriteLine to Write in the JScript code if you want to write strings without the newline characters.

@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment
:: -------- Batch code --------------
@echo off
call :jWrite "x66x6fx6f" >test.txt
exit /b

:jWrite
cscript.exe //E:JScript //nologo "%~f0" %1
exit /b
:: --------- JScript code -----------*/
WScript.StdOut.WriteLine(eval('"'+WScript.Arguments.Unnamed(0)+'"'));

优点:

  • 从 XP 开始在所有 Windows 机器上本地运行的纯脚本
  • 非常简单的 JScript 代码
  • 可以一次性处理整个字符串
  • 许多其他可用的转义序列,例如 \ 等.
  • 容易将普通文本与转义序列混合,但双引号必须是x22
  • Pure script that runs natively on all Windows machines from XP onward
  • Very simple JScript code
  • Can process entire strings in one pass
  • Many other escape sequences available, such as \, , , , etc.
  • Easy to mix ordinary text with escape sequences, but double quote must be x22

缺点:

  • 并非所有高位字节都能给出正确的结果
  • 结果取决于您机器的默认字符集.最好是 Windows 1252

只要您的机器默认为 Windows-1252,编写一些 JScript 代码来正确解释所有 xNN 代码以提供正确的字节并不难.如果您的命令会话的活动代码页也与 Windows 1252 匹配,那么您可以自由混合普通文本.

It is not too difficult to write some JScript code to properly interpret all xNN codes to give the correct byte as long as your machine defaults to Windows-1252. And if your command session's active code page also matches Windows 1252, then you can freely mix in normal text.

@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment
:: -------- Batch code --------------
@echo off
call :jWrite "x66x6fx6f"
call :jWrite "Hello
worldx80"
exit /b

:jWrite
cscript.exe //E:JScript //nologo "%~f0" %1
exit /b
:: --------- JScript code -----------*/
WScript.StdOut.WriteLine(WScript.Arguments.Unnamed(0).replace(
  /\(\|b|f|n|r|t|v|x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4})/g,
  function($0,$1) {
    switch ($1.toLowerCase()) {
      case 'x80': return 'u20AC';
      case 'x82': return 'u201A';
      case 'x83': return 'u0192';
      case 'x84': return 'u201E';
      case 'x85': return 'u2026';
      case 'x86': return 'u2020';
      case 'x87': return 'u2021';
      case 'x88': return 'u02C6';
      case 'x89': return 'u2030';
      case 'x8a': return 'u0160';
      case 'x8b': return 'u2039';
      case 'x8c': return 'u0152';
      case 'x8e': return 'u017D';
      case 'x91': return 'u2018';
      case 'x92': return 'u2019';
      case 'x93': return 'u201C';
      case 'x94': return 'u201D';
      case 'x95': return 'u2022';
      case 'x96': return 'u2013';
      case 'x97': return 'u2014';
      case 'x98': return 'u02DC';
      case 'x99': return 'u2122';
      case 'x9a': return 'u0161';
      case 'x9b': return 'u203A';
      case 'x9c': return 'u0153';
      case 'x9d': return 'u009D';
      case 'x9e': return 'u017E';
      case 'x9f': return 'u0178';
      default:    return eval('"'+$0+'"');
    }
  }
));

优点:

  • 从 XP 开始在所有 Windows 机器上本地运行的纯脚本
  • 可以一次性处理整个字符串
  • 支持从 x00 到 xFF 的所有字节码
  • 许多其他可用的转义序列,例如 \ 等.
  • 容易将普通文本与转义序列混合,但双引号必须是x22
  • Pure script that runs natively on all Windows machines from XP onward
  • Can process entire strings in one pass
  • All bytes codes from x00 through xFF are supported
  • Many other escape sequences available, such as \, , , , etc.
  • Easy to mix ordinary text with escape sequences, but double quote must be x22

缺点:

  • 仅当您的机器默认为 Windows-1252 时,才能为所有字节提供正确的结果
  • 中等复杂的 JScript 代码

我的 JREPL.BAT 实用程序最初是设计的对文本文件执行正则表达式搜索和替换操作.但它有一些选项允许它轻松编写带有嵌入转义序列的字符串,并且无论您的机器使用什么默认字符集,它都可以为所有字节码提供正确的结果.

My JREPL.BAT utility was originally designed to perform regular expression search and replace operations on text files. But it has options that allow it to easily write strings with embedded escape sequences, and it can give the correct result for all byte codes no matter what default character set your machine uses.

如果您的机器默认使用任何单字节字符集,那么您可以安全地使用以下从 x00 到 xFF 的所有可能转义序列,并且您可以自由地将普通文本与转义序列混合在一起.

If your machine defaults to any single byte character set, then you can safely use the following with all possible escape sequences from x00 through xFF, and you can freely mix in normal text along with escape sequences.

call jrepl $ "x66x6fx6f" /s "=" /x /o test.txt

/s "=" 选项指定一个未定义的环境变量作为输入源,它被解释为一个空字符串.第一个 $ 参数匹配空字符串的结尾.第二个 "x66x6fx6f" 参数指定替换值./x 选项启用替换字符串中的转义序列,/o test.txt 选项指定输出文件.

The /s "=" option specifies an undefined environment variable as the input source, which is interpreted as an empty string. The first $ argument matches the end of the empty string. The second "x66x6fx6f" argument specifies the replacement value. The /x option enables escape sequences within the replacement string, and the /o test.txt option specifies the output file.

如果要附加到 test.txt,则添加 /APP 选项.

If you want to append to test.txt, then add the /APP option.

如果你想要 行尾而不是 ,(Unix 风格而不是 Windows)然后添加 /U 选项

If you want end-of-line instead of , (Unix style instead of Windows) then add the /U option

如果您不需要任何换行符,请添加 /M 选项.

If you don't want any new line terminators, then add the /M option.

最后,如果您的机器不默认使用单字节字符集,您仍然可以通过为输出格式指定像 Windows-1252 这样的单字节字符集来强制所有转义序列的正确结果.但是,如果指定的字符集与您的命令会话的活动代码页不匹配,则只能保证转义序列有效 - 一些正常的文本字符可能会给出错误的结果.

Lastly, if your machine does not default to a single byte character set, you can still force the correct result for all escape sequences by specifying a single byte character set like Windows-1252 for the output format. However, if the specified character set does not match your command session's active code page, then only escape sequences are guaranteed to work - some normal text characters may give the wrong result.

call jrepl $ "x66x6fx6f" /s "=" /x /o "test.txt|Windows-1252"

优点:

  • 从 XP 开始可在任何 Windows 机器上运行的纯脚本
  • 可以一次性处理整个字符串
  • 支持从 x00 到 xFF 的所有字节码
  • 许多其他可用的转义序列,例如 \ 等.
  • 容易将普通文本与转义序列混合,但双引号必须是 x22q
  • 默认字符集不可知 - 总能给出正确的结果
  • 在您的工具库中加入 JREPL 后,您会发现它有很多用途.这是一个强大的工具.
  • Pure script that runs on any Windows machine from XP onward
  • Can process entire strings in one pass
  • All bytes codes from x00 through xFF are supported
  • Many other escape sequences available, such as \, , , , etc.
  • Easy to mix ordinary text with escape sequences, but double quote must be x22 or q
  • Default character set agnostic - can always give the correct result
  • Once you have JREPL in your arsenal of tools, you will find many uses for it. It is a powerful tool.

缺点:

  • 需要下载第 3 方脚本

这篇关于在 Windows 批处理中将 HEX 值写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆