This page is edited from the 1cmdfaq.txt faq-file contained in
my tscmd.zip
command line interface (CLI) collection. That zipped file has much
additional material, including a number of detached .cmd script
files. It is recommended that you also get the zipped version as a
companion.
Please see "
The Description and
the Index page" for the conditions of usage and other such
information.
33} How can I convert a binary, octal, hexadecimal into a decimal?
@echo off & setlocal enableextensions
::
set bin_=1001110
call :BinToDecimal %bin_% decimal_
echo Binary %bin_% = decimal %decimal_%
::
set oct_=116
call :OctToDecimal %oct_% decimal_
echo Octal %oct_% = decimal %decimal_%
::
set hex_=4E
call :HexToDecimal %hex_% decimal_
echo Hexadecimal %hex_% = decimal %decimal_%
endlocal & goto :EOF
::
::===============================================================
:BinToDecimal BinaryIn DecimalOut
::
setlocal enableextensions
set bin_=%1
set /a dec_=0
:_loop
set first_=%bin_:~0,1%
set /a dec_=%first_%+2*%dec_%
set bin_=%bin_:~1%
if defined bin_ goto _loop
endlocal & set %2=%dec_% & goto :EOF
::
::===============================================================
:OctToDecimal OctIn DecimalOut
::
setlocal enableextensions
set /a oct_=0%1
endlocal & set %2=%oct_% & goto :EOF
::
::===============================================================
:HexToDecimal HexIn DecimalOut
::
setlocal enableextensions
set /a hex_=0x%1
endlocal & set %2=%hex_% & goto :EOF
The output will be
D:\TEST>cmdfaq
Binary 1001110 = decimal 78
Octal 116 = decimal 78
Hexadecimal 4E = decimal 78
A true application which I needed:
@echo off & setlocal enableextensions
::
:: Requires a parameter (in hexadecimal)
if [%1]==[] goto _usage
::
:: Put the parameter into a regular environment variable
set hex_=%1
::
:: The require format of the parameter is #C1C2C2
:: Where C1 is the first color in hexadecomal and so on
set h0_=%hex_:~0,1%
if not [%h0_%]==[#] goto _usage
::
:: Check that the parameter is not too long
set h4_=%hex_:~7,1%
if defined h4_ goto _usage
::
:: Decompose the parameter into three parts with two characters each
set h1_=%hex_:~1,2%
set h2_=%hex_:~3,2%
set h3_=%hex_:~5,2%
::
:: Convert the hexadecimals
set /a d1_=0x%h1_%
set /a d2_=0x%h2_%
set /a d3_=0x%h3_%
::
:: Display the results
echo %h1_% %h2_% %h3_%
echo %d1_% %d2_% %d3_%
goto :EOF
::
:_usage
echo +----------------------------------------------------+
echo ^| Covert a hexadecimal color code into decimal ^|
echo ^| By Prof. Timo Salmi, Last modified Fri 29-Apr-2005 ^|
echo +----------------------------------------------------+
echo.
echo Usage %~f0 #C1C2C3
echo.
echo Where C1 is the first color in hexadecimal and so on
goto :EOF
The output might be
C:\_D\TEST>cmdfaq #ff1faa
ff 1f aa
255 31 170
If one wants a better error handling, substitute
::
:: Convert the hexadecimals, check the hex input for validity
set /a d1_=0x%h1_% || (
echo. & echo Invalid hexadecimal %h1_% in %hex_% & goto :EOF)
set /a d2_=0x%h2_% || (
echo. & echo Invalid hexadecimal %h2_% in %hex_%
goto :EOF)
set /a d3_=0x%h3_% || (
echo.
echo Invalid hexadecimal %h3_% in %hex_%
goto :EOF)
Note at the same time the demonstrations of different formats. For
more on the conditional processing symbol || see
hh ntcmds.chm::/ntcmds_shelloverview.htm [You can't use this unless you have the ntcmds.chm file]
"run the command following || only if the command preceding || fails"
Also see
Item #73
and
Item #88.