echo The error level is: %ERRORLEVEL%
生产
>The error level is: 15
我想要什么:
>The error level is: F
我是否需要进行转换,或者有没有办法以不同的方式显示数字?
感谢正确方向的任何帮助。
以前的事了,我很无聊。
cmdcalc.cmd
@echo off
if not defined trace set trace=rem
%trace% on
SetLocal
if "%1"=="/?" (
call :help %0
goto :eof
)
Set MinInBase=
if /i "%2" EQU "Bin" call :DoBin %1
if /i "%2" EQU "Hex" call :DoHex %1
If not defined BinStr call :DoDec %1
EndLocal & set RET=%RET%
goto :eof
:DoBin
Set MinInBase=2
Set ShiftBy=1
Set StartSyn=0b
call :DoCalc %1
goto :eof
:DoHex
Set MinInBase=16
Set ShiftBy=4
Set StartSyn=0x
call :DoCalc %1
goto :eof
:DoDec
if {%1} EQU {} goto :eof
set /a BinStr=%1
set RET=%BinStr%
echo %RET%
goto :eof
:DoCalc
Set BinStr=
SET /A A=%1
%Trace% %A%
:StartSplit
SET /A B="A>>%ShiftBy%"
%Trace% %B%
SET /A C="B<<%ShiftBy%"
%Trace% %C%
SET /A C=A-C
%Trace% %C%
call :StringIt %C%
If %B% LSS %MinInBase% goto :EndSplit
set A=%B%
goto :StartSplit
:EndSplit
call :StringIt %B%
set RET=%StartSyn%%BinStr%
Echo %RET%
EndLocal & set RET=%RET%
goto :eof
:StringIt
set Bin=0123456789ABCDEF
FOR /F "tokens=*" %%A in ('echo "%%BIN:~%1,1%%"') do set RET=%%A
set ret=%ret:"=%
Set BinStr=%Ret%%BinStr%
goto :eof
:help
echo %1 syntax:
echo.
echo %1 Calculation [Hex^|Bin]
echo.
echo eg %1 12*2 Hex
echo.
echo gives 0x18.
goto :eof
根据外部资源 Windows 环境变量,有一个未记录的内置只读变量=ExitCode
它以十六进制格式返回当前退出代码。要确保ErrorLevel
值等于退出代码,请使用 cmd /C exit %ErrorLevel%
。
因此,如果您使用此行代码...:
cmd /C exit %ErrorLevel%
echo The error level is: %=ExitCode%
。您将收到此消息(假设ErrorLevel
15
(:
The error level is: 0000000F
要摆脱前导零,请使用这个...:
cmd /C exit %ErrorLevel%
for /F "tokens=* delims=0" %%Z in ("%=ExitCode%") do set "HEXCODE=%%Z"
if not defined HEXCODE set "HEXCODE=0"
echo The error level is: %HEXCODE%
。要得到这个:
The error level is: F
只需将其编写在 vbscript 而不是批处理中即可
将下面的 VBScript 语句放入一个文件中。
WScript.Echo Hex( WScript.Arguments(0) )
然后要运行它,只需在命令行上键入 this(在批处理脚本中,如果需要,请使用 for 循环来捕获值(
C:workspace> cscript //nologo hex.vbs 15
F
无需安装任何东西。 VBScript在大多数Windows系统中都是默认的。
perl -e"printf qq{The errorlevel is: %Xn}, $ENV{ERRORLEVEL}"
当然,需要安装Perl,但这很容易做到。