Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
765 views
in Technique[技术] by (71.8m points)

batch file - echo -e equivalent in Windows?

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 "Hellox01x02x03" and you get Hello???.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...