echo -e 在 Windows 中等效吗?

发布于 2024-10-08 13:20:52 字数 87 浏览 0 评论 0 原文

Windows 中是否有等效的 Linux“echo -e”,以便我可以使用“echo -e \xnnn”打印出 ASCII 代码为十六进制值 nnn 的字符?

Is there any Linux "echo -e" equivalent in windows so I can use "echo -e \xnnn" to print out the character whose ASCII code is the hexadecimal value nnn ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

把梦留给海 2024-10-15 13:20:52

没有等效的函数,但您可以编写自己的函数。

我会把问题分成两部分。

  • 将十六进制数转换为十进制数。
  • 将十进制数转换为 ASCII。

从十六进制转换为十进制很简单 将

set "myHex=4A"
set /a decimal=0x%myHex%

数字转换为 ASCII 比较棘手,这取决于所需的 ASCII 范围
如果您只需要从 32(空格)到 126'~' 的范围,您可以使用 =ExitCodeAscii 变量

set "decimal=%1"
cmd /c exit /b %decimal%
echo "%=ExitCodeAscii%"

如果您还需要一些特殊字符,例如 CRLFESCDEL,可以用纯批处理创建它们。

如果您需要更多,可以使用 vbscript。

forfiles 方法

更通用的方法是使用命令 forfiles (Dostips: 生成几乎任何字符...)

echo-e.bat

@echo off
set "arg1=%~1"
set "arg1=%arg1:\x=0x%"
forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(%arg1%"

你可以通过 echo-e.bat "Hello\x01\x02\x03"< /代码>
你会得到Hello☺☻♥

There is no equivalent, but you can write your own function.

I would split the problem into two parts.

  • Convert the hex number to decimal.
  • Convert the decimal number to ASCII.

Converting from hex to decimal is simple by

set "myHex=4A"
set /a decimal=0x%myHex%

Converting a number to an ASCII is more tricky, it depends of the required ASCII-range
If you only need the range from 32 (space) to 126'~', you could use the =ExitCodeAscii variable

set "decimal=%1"
cmd /c exit /b %decimal%
echo "%=ExitCodeAscii%"

If you need also some special characters like CR, LF, ESC, DEL, it is possible to create them with pure batch.

If you need more than you could use vbscript.

forfiles method

A more general way is to use the command forfiles (Dostips: Generate nearly any character...)

echo-e.bat

@echo off
set "arg1=%~1"
set "arg1=%arg1:\x=0x%"
forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(%arg1%"

You can call it via echo-e.bat "Hello\x01\x02\x03"
and you get Hello☺☻♥.

还在原地等你 2024-10-15 13:20:52

Dave Benham 编写了一个名为 CharLib 的批处理文件字符操作库。
除其他外,该库能够将 1..255 范围内的所有字符(它不能转换为 0 -> NUL)的十六进制转换为 ascii。

有关详细信息,请参阅 是否可以将字符转换为其 ASCII 值? ;t=2610" rel="noreferrer">DosTips.com

如果您只需要在批处理脚本中使用单个有问题的字符(例如控制代码),那么它对于剪切和粘贴来说是非常宝贵的资源。

例如,在批处理脚本中打印 CR 字符(不带 LF)...

@echo off
setlocal enableextensions enabledelayedexpansion
for /f %%a in ('copy /Z "%~dpf0" nul') do set "ASCII_13=%%a"
echo 1234567890!ASCII_13!xxxxxx

最终将按预期在控制台上打印“xxxxxx7890”。

There is a batch file character manipulation library written by Dave Benham called CharLib.
This library, amongst other things, is able to convert hex to ascii, for all characters in the range 1..255 (it can't to 0 -> NUL).

For more info see the thread Is it possible to convert a character to its ASCII value? on DosTips.com

If you just need to use a single problematic character (e.g. a control code) in your batch script, then it's an invaluable resource for a cut and paste.

e.g. to print a CR character (without LF) in a batch script...

@echo off
setlocal enableextensions enabledelayedexpansion
for /f %%a in ('copy /Z "%~dpf0" nul') do set "ASCII_13=%%a"
echo 1234567890!ASCII_13!xxxxxx

will end up printing "xxxxxx7890" on the console as expected.

魔法唧唧 2024-10-15 13:20:52

这是一个简单的混合批处理/JScript 文件,可用于回显任何有效 JScript 表达式的结果,包括使用 \xnn 的表达式。它支持从 \x00 到 \xFF 的所有字节码。

该批处理采用用双引号括起来的单个参数。参数应该是有效的 JScript 表达式。双引号不是表达式的一部分。表达式中的 JScript 字符串应括在单引号内。

默认情况下,换行符不会打印在结果末尾。 /N 选项用于将换行符附加到结果的末尾。

如果在批处理文件中使用,那么当然应该使用 CALL 来调用脚本。

jEval.bat

@if (@X)==(@Y) @end /* harmless hybrid line that begins a JScrpt comment

::: Batch part ::::
@echo off
cscript //nologo //e:JScript "%~f0" %*
exit /b

*** JScript part ***/
if (WScript.Arguments.Named.Exists("n")) {
  WScript.StdOut.WriteLine(eval(WScript.Arguments.Unnamed(0)));
} else {
  WScript.StdOut.Write(eval(WScript.Arguments.Unnamed(0)));
}

这是一个示例调用,它使用 \xnn 在输出中引入新行,并包含一些浮点数学。

>jEval " 'Hello world!\x0a4/5=' + 4/5 " /n
Hello world!
4/5=0.8

>

Here is a simple hybrid batch/JScript file that can be used to echo the results of any valid JScript expression, including one using \xnn. It supports all byte codes from \x00 through \xFF.

The batch takes a single argument enclosed in double quotes. The argument should be a valid JScript expression. The double quotes are not part of the expression. JScript strings within the expression should be enclosed within single quotes.

By default, a newline is not printed at the end of the result. The /N option is used to append a newline to the end of the result.

If used within a batch file, then of course the script should be called using CALL.

jEval.bat

@if (@X)==(@Y) @end /* harmless hybrid line that begins a JScrpt comment

::: Batch part ::::
@echo off
cscript //nologo //e:JScript "%~f0" %*
exit /b

*** JScript part ***/
if (WScript.Arguments.Named.Exists("n")) {
  WScript.StdOut.WriteLine(eval(WScript.Arguments.Unnamed(0)));
} else {
  WScript.StdOut.Write(eval(WScript.Arguments.Unnamed(0)));
}

Here is a sample call that uses \xnn to introduce a new line in the output, as well as incorporates some floating point math.

>jEval " 'Hello world!\x0a4/5=' + 4/5 " /n
Hello world!
4/5=0.8

>
无畏 2024-10-15 13:20:52

添加到@jeb所说的你可以重新创建

echo -n -e "win\x01\x02\x03\x04" > file.exe

@echo off
setlocal
set LF=^


set "arg1=%~1"
set "arg1=%arg1:\x=0x%"
for /f eol^=^%LF%%LF%^ delims^= %%A in (
    'forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo|set /p=%arg1%"'
) do if "%~2" neq "" (set %~2=%%A) else echo|set /p=%%A

变成:

echone.bat "win\x01\x02\x03\x04" > file.exe

Adding to what @jeb said you can recreate

echo -n -e "win\x01\x02\x03\x04" > file.exe

with

@echo off
setlocal
set LF=^


set "arg1=%~1"
set "arg1=%arg1:\x=0x%"
for /f eol^=^%LF%%LF%^ delims^= %%A in (
    'forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo|set /p=%arg1%"'
) do if "%~2" neq "" (set %~2=%%A) else echo|set /p=%%A

which becomes:

echone.bat "win\x01\x02\x03\x04" > file.exe
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文