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
How do I display integers in hexadecimal, from a batch file?
提问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 =ExitCode
which returns the current exit code in hexadecimal format. To ensure the ErrorLevel
value 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 ErrorLevel
is 15
):
...你会收到这个(假设ErrorLevel
是15
):
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 系统中都是默认的。