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 ?
4 Answers
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☺☻♥
.
-
3Thanks for the tip! It's great to learn about `%=ExitCodeAscii%` and other obscure [Dynamic Environment Variables](http://www.robvanderwoude.com/ntset.php#DynamicEnvironmentVariables)… – mousio Apr 07 '11 at 19:38
-
2For some reason the "well known" `=ExitCodeAscii` was well hidden from me until now. :) Thanks for this great piece of knowledge! – Andriy M Apr 05 '13 at 09:56
-
and what is the equivalent of echo -ne \x01\x02\x03\x04 ?? – Zibri Dec 08 '16 at 22:16
-
@Zibri I added the solution for `-e`. For `-n` you should search for `echo without newline` – jeb Dec 09 '16 at 10:59
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.

- 22,706
- 18
- 63
- 99

- 403
- 6
- 4
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
>

- 127,446
- 28
- 251
- 390
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

- 21
- 1