如何从批处理文件中以十六进制显示整数?

T.T*_*.T. 2 windows math integer batch-file

echo The error level is: %ERRORLEVEL%
Run Code Online (Sandbox Code Playgroud)

产生

>The error level is: 15
Run Code Online (Sandbox Code Playgroud)

我想要的是什么:

>The error level is: F
Run Code Online (Sandbox Code Playgroud)

我是否需要进行转换,还是有办法以不同的方式显示数字?

感谢您对正确方向的任何帮助表示赞赏.

And*_*ris 9

很久以前,我很无聊.

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
Run Code Online (Sandbox Code Playgroud)

  • +1,这个问题有点矫枉过正,但解决方案 (3认同)

asc*_*pfl 7

根据外部资源Windows Environment Variables,有一个未记录的内置只读变量=ExitCode,它以十六进制格式返回当前退出代码.要确保该ErrorLevel值等于退出代码,请使用cmd /C exit %ErrorLevel%.

所以如果你使用这个行代码...:

cmd /C exit %ErrorLevel%
echo The error level is: %=ExitCode%
Run Code Online (Sandbox Code Playgroud)

......你会收到这个(假设ErrorLevel15):

The error level is: 0000000F
Run Code Online (Sandbox Code Playgroud)

要摆脱前导零,请使用此...:

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%
Run Code Online (Sandbox Code Playgroud)

......得到这个:

The error level is: F
Run Code Online (Sandbox Code Playgroud)