C:\_G\WWW\~ELISANET\INFO\tscmd033.html
<https://www.salminet.fi/info/tscmd033.html>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Wed 22-Jul-2026 14:38:26

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

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"

Back to the original, now with PowerShell-aided cmd.exe command-line alternatives: (No checking of input errors)
  @echo off & setlocal enableextensions
  set bin_=1001110
  call :BinToDecimalPS %bin_% decimal_
  echo Binary %bin_% = decimal %decimal_%
  set oct_=116
  call :OctToDecimalPS %oct_% decimal_
  echo Octal %oct_% = decimal %decimal_%
  set hex_=4E
  call :HexToDecimalPS %hex_% decimal_
  echo Hexadecimal %hex_% = decimal %decimal_%

  :BinToDecimalPS BinaryIn DecimalOut
  setlocal enableextensions
  for /f "usebackq delims=" %%d in (`powershell -command ^
    "[Convert]::ToInt64('%1',2)"`) do (
      set dec_=%%d)
  endlocal & set "%2=%dec_%" & goto :EOF

  :OctToDecimalPS OctalIn DecimalOut
  setlocal enableextensions
  for /f "usebackq delims=" %%d in (`powershell -command ^
    "[Convert]::ToInt64('%1',8)"`) do (
      set dec_=%%d)
  endlocal & set "%2=%dec_%" & goto :EOF

  :HexToDecimalPS HexIn DecimalOut
  setlocal enableextensions
  for /f "usebackq delims=" %%d in (`powershell -command ^
    "[Convert]::ToInt64('%1',16)"`) do (
      set dec_=%%d)
  endlocal & set "%2=%dec_%" & goto :EOF

The output will be
  D:\TEST>cmdfaq
  Binary 1001110 = decimal 78
  Octal 116 = decimal 78
  Hexadecimal 4E = decimal 78

  @rem other potential combinations. No error checking.
  @echo off & setlocal enableextensions
  set bin_=1001110
  call :BinToHexPS %bin_% hex_
  echo %bin_% in bin is %hex_% in 16
  endlocal & goto :EOF

  :BinToHexPS
  setlocal enableextensions
  for /f "usebackq delims=" %%h in (`powershell -command ^
    "[Convert]::ToString([Convert]::ToInt64('%1',2),16).ToUpper()"`) do (
      set result_=%%h)
  endlocal & set "%2=%result_%" & goto :EOF

The output will be
  D:\TEST>cmdfaq
  1001110 in bin is 4E in hex

Also see Item #32, Item #73 and Item #88.

References/Comments: (If a Google message link fails try the links within the brackets.)
  Google Groups Apr 29 2005, 3:46 pm [M]
  Google Groups Apr 30 2005, 9:32 am [M]
  Number converter - hex, octal, binary | Coder’s Toolbox

[Previous] [Next]

C:\_G\WWW\~ELISANET\INFO\tscmd033.html
C:\_G\WWW\~ELISANET\FTPCMD\TSALMI.CMD /tscmd033
C:\_E\ARCZIP\COPYTONE.CMD /~info
https://www.salminet.fi/info/tscmd033.html
file:///c:/_g/www/~elisanet/info/tscmd033.html