<http://www.netikka.net/tsneti/info/tscmd032.htm>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Tue 30-Dec-2014 18:19:33

 
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.



32} How can I convert a decimal number to binary, octal, hexadecimal?

  @echo off & setlocal enableextensions
  set decimal_=78
  call :DecimalToBinary %decimal_% binary_
  echo Decimal %decimal_% = Binary %binary_%
  ::
  call :DecimalToOctal %decimal_% oct_
  echo Decimal %decimal_% = Octal %oct_%
  ::
  call :DecimalToHex %decimal_% hex_
  echo Decimal %decimal_% = Hexadecimal %hex_%
  endlocal & goto :EOF
  ::
  ::===============================================================

  :DecimalToBinary DecimalIn BinaryOut
  ::
  setlocal enableextensions
  set /a dec_=%1
  if %dec_% LSS 0 (endlocal & set %2=Error: negative & goto :EOF)
  set bin_=
  :_binloop
    set /a dec1_=%dec_%
    set /a dec_=%dec_% / 2
    set /a bindigit_=%dec1_% - 2*%dec_%
    set bin_=%bindigit_%%bin_%
    if %dec_% GTR 0 goto :_binloop
  endlocal & set %2=%bin_% & goto :EOF
  ::
  ::===============================================================

  :DecimalToOctal DecimalIn OctalOut
  ::
  setlocal enableextensions
  set /a dec_=%1
  if %dec_% LSS 0 (endlocal & set %2=Error: negative & goto :EOF)
  set oct_=
  :_octloop
    set /a dec1_=%dec_%
    set /a dec_=%dec_% / 8
    set /a octdigit_=%dec1_% - 8*%dec_%
    set oct_=%octdigit_%%oct_%
    if %dec_% GTR 0 goto :_octloop
  endlocal & set %2=%oct_% & goto :EOF
  ::
  ::===============================================================

  :DecimalToHex DecimalIn HexOut
  ::
  setlocal enableextensions
  set /a dec_=%1
  if %dec_% LSS 0 (endlocal & set %2=Error: negative & goto :EOF)
  set hex_=
  :_hexloop
    set /a dec1_=%dec_%
    set /a dec_=%dec_% / 16
    set /a hexdigit_=%dec1_% - 16*%dec_%
    set /a hexidx_=%hexdigit_%+1
    for /f "tokens=%hexidx_%" %%h in (
      'echo 0 1 2 3 4 5 6 7 8 9 A B C D E F') do set hexdigit_=%%h
    set hex_=%hexdigit_%%hex_%
    if %dec_% GTR 0 goto :_hexloop
  endlocal & set %2=%hex_% & goto :EOF

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

References/Comments: (If a Google message link fails try the links within the brackets.)
  Google Groups Apr 27 2010, 8:12 pm [M]
  Number converter - hex, octal, binary | Coder’s Toolbox