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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5737658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:41:36  来源:igfitidea点击:

How do I display integers in hexadecimal, from a batch file?

windowsmathbatch-fileinteger

提问by T.T.T.

echo The error level is: %ERRORLEVEL%

Produces

生产

>The error level is: 15

What I would like:

我想要什么:

>The error level is: F

do I need to do conversions or is there a way to display numbers differently?

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

Any help in the right direction is appreciated, thanks.

感谢您对正确方向的任何帮助,谢谢。

回答by Andy Morris

It was a long time ago, I was very bored.

很久以前,我很无聊。

cmdcalc.cmd

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

回答by aschipfl

According to the external resource Windows Environment Variables, there is an undocumented built-in read-only variable =ExitCodewhich returns the current exit code in hexadecimal format. To ensure the ErrorLevelvalue equals the exit code, use cmd /C exit %ErrorLevel%.

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

So if you are using this line code...:

因此,如果您正在使用此行代码...:

cmd /C exit %ErrorLevel%
echo The error level is: %=ExitCode%

...you will receive this (supposing the ErrorLevelis 15):

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

The error level is: 0000000F
The error level is: 0000000F

To get rid of the leading zeros, use this...:

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

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%

...to get this:

...得到这个:

The error level is: F
The error level is: F

回答by bash-o-logist

Just write it in vbscript instead of batch

只需在 vbscript 中而不是批处理中编写它

put the vbscript statement below into a file.

将下面的 vbscript 语句放入一个文件中。

WScript.Echo Hex( WScript.Arguments(0) )

then to run it, simple type this on the command line ( in a batch script, use a for loop to capture the value if required )

然后运行它,在命令行上简单地输入这个(在批处理脚本中,如果需要,使用 for 循环来捕获值)

C:\workspace> cscript //nologo hex.vbs 15
F

There is no need to install anything. vbscript comes by default in most windows systems.

无需安装任何东西。vbscript 在大多数 Windows 系统中都是默认的。

回答by ikegami

perl -e"printf qq{The errorlevel is: %X\n}, $ENV{ERRORLEVEL}"

Requires Perlto be installed, of course, but that's easy to do.

当然,需要安装Perl,但这很容易做到。