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 ?
There is no equivalent, but you can write your own function.
I would split the problem into two parts.
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.
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☺☻♥
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With