echo -e 在 Windows 中等效吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4495576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
echo -e equivalent in Windows?
提问by ccy
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 ?
在 Windows 中是否有任何等效的 Linux“echo -e”,因此我可以使用“echo -e \xnnn”打印出其 ASCII 代码为十六进制值 nnn 的字符?
回答by jeb
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.
- 将十六进制数转换为十进制数。
- 将十进制数转换为 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
将数字转换为 ASCII 更棘手,这取决于所需的 ASCII 范围
如果您只需要 32(空格)到 126'~' 的范围,则可以使用=ExitCodeAscii
变量
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.
如果您还需要一些特殊字符,例如CR
, LF
, ESC
, DEL
,则可以使用纯批处理来创建它们。
If you need more than you could use vbscript.
如果您需要的不仅仅是 vbscript。
forfiles method
forfiles 方法
A more general way is to use the command forfiles
(Dostips: Generate nearly any character...)
更通用的方法是使用命令forfiles
(Dostips: Generate几乎所有字符...)
echo-e.bat
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???
.
你可以通过调用它echo-e.bat "Hello\x01\x02\x03"
,你会得到Hello???
.
回答by timfoden
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).
有一个由 Dave Benham 编写的批处理文件字符操作库,名为CharLib。该库除其他外,能够将十六进制转换为 ascii,适用于 1..255 范围内的所有字符(它不能转换为 0 -> NUL)。
For more info see the thread Is it possible to convert a character to its ASCII value?on DosTips.com
有关更多信息,请参阅线程是否可以将字符转换为其 ASCII 值?在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...
例如在批处理脚本中打印一个 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
will end up printing "xxxxxx7890" on the console as expected.
最终将按预期在控制台上打印“xxxxxx7890”。
回答by mk7
Adding to what @jeb said you can recreate
添加到@jeb 所说的你可以重新创建
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
回答by dbenham
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.
这是一个简单的混合批处理/JScript 文件,可用于回显任何有效 JScript 表达式的结果,包括使用 \xnn 的表达式。它支持从\x00 到\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.
批处理采用用双引号括起来的单个参数。参数应该是有效的 JScript 表达式。双引号不是表达式的一部分。表达式中的 JScript 字符串应该用单引号括起来。
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.
默认情况下,结果末尾不打印换行符。/N 选项用于在结果末尾追加换行符。
If used within a batch file, then of course the script should be called using CALL.
如果在批处理文件中使用,那么当然应该使用 CALL 调用脚本。
jEval.bat
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.
这是一个示例调用,它使用 \xnn 在输出中引入一个新行,并结合了一些浮点数学。
>jEval " 'Hello world!\x0a4/5=' + 4/5 " /n
Hello world!
4/5=0.8
>