(view source code of bintodec.bat as plain text)
@ECHO OFF
SETLOCAL
:: Check number of command line arguments: 1 and only 1 required
IF "%~1"=="" GOTO Syntax
IF NOT "%~2"=="" GOTO Syntax
:: Check if the command line argument consists of zeroes and ones only
FOR /F "delims=01" %%A IN ("%~1") DO GOTO Syntax
:: Initialize the variables
SET Binary=%~1
SET BitVal=1
SET Decimal=0
:: Display the initial binary value
SET Binary
:Loop
SET Bit=%Binary:~-1%
SET Binary=0%Binary:~0,-1%
SET /A Decimal += %BitVal% * %Bit% || GOTO Syntax
SET /A BitVal *= 2 || GOTO Syntax
IF %Binary% GTR 0 GOTO Loop
:: Display the decimal result
SET Decimal
:: Exit with the decimal result as return code
ENDLOCAL & EXIT /B %Decimal%
:Syntax
ECHO.
ECHO BinToDec.bat, Version 1.00
ECHO Convert binary numbers to decimal
ECHO.
ECHO Usage: BIN2DEC binary_number
ECHO.
ECHO Where: "binary_number" is a binary number to be converted
ECHO (zeroes and ones only, maximum 31 bits)
ECHO.
ECHO Notes: The binary number and the decimal result are displayed
ECHO as text on screen, and the decimal number is returned
ECHO as "errorlevel" (return code).
ECHO Return code is -1 in case of errors.
ECHO.
ECHO Written by Rob van der Woude
ECHO https://www.robvanderwoude.com
ENDLOCAL
EXIT /B -1
page last modified: 2024-04-16; loaded in 0.0050 seconds