Skip to main content

How to Convert a Number to a Different Base (Base 2–36) in Batch Script

Number systems are the foundation of computing. While most everyday work uses Base-10 (Decimal), systems use Base-2 (Binary), Base-8 (Octal), Base-16 (Hexadecimal), and sometimes even Base-36 (which uses digits 0-9 and letters A-Z). A General Base Converter allows you to convert any decimal number into any base from 2 to 36 using a single reusable algorithm.

In this guide, we will demonstrate how to build a universal base converter using the "Repeated Division" method.

The Strategy: Divide and Collect Remainders

To convert a decimal number to Base $B$:

  1. Divide the number by $B$.
  2. Record the remainder (this is the rightmost digit in the new base).
  3. Replace the number with the quotient.
  4. Repeat until the number is 0.
  5. The result is the remainders read in reverse order.

Implementation Script

@echo off
setlocal enabledelayedexpansion

set /p "dec=Enter decimal number: "
set /p "base=Enter target base (2-36): "

:: 1. Define the Digit Characters (0-9, A-Z)
set "DIGITS=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

:: 2. Validate Base
if !base! LSS 2 (echo [ERROR] Base must be 2-36. & pause & exit /b)
if !base! GTR 36 (echo [ERROR] Base must be 2-36. & pause & exit /b)

:: 3. Handle negative numbers
set "sign="
set "num=!dec!"
if !num! LSS 0 (
set "sign=-"
set /a "num=0 - num"
)

:: 4. Handle zero
if !num! EQU 0 (
set "result=0"
goto :show_result
)

:: 5. Conversion Loop
set "result="

:convert_loop
if !num! EQU 0 goto :show_result

set /a "rem=num %% base"
set /a "num/=base"

:: Get the character for this remainder
set "char=!DIGITS:~%rem%,1!"

:: Prepend it to the result (building right to left)
set "result=!char!!result!"

goto :convert_loop

:show_result
echo.
echo ==========================================
echo DECIMAL: !dec!
echo BASE !base!: !sign!!result!
echo ==========================================
pause
How the Digit Lookup Works

The string DIGITS contains all 36 possible digit characters in order: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ. The remainder from each division (0–35) is used as a substring offset (!DIGITS:~%rem%,1!) to extract the corresponding character. For example, remainder 10 extracts A, remainder 15 extracts F, and remainder 35 extracts Z.

Substring Expansion Mixing

The expression !DIGITS:~%rem%,1! intentionally mixes percent and delayed expansion. The %rem% is expanded first during the parse phase of each goto iteration, providing the numeric offset. Then !DIGITS:~N,1! is resolved via delayed expansion to extract the character. This works because each pass through goto :convert_loop triggers a fresh parse of the block.

Why Use a General Base Converter?

  1. Hexadecimal: Base-16 is used for color codes (#FF5733), memory addresses, and MAC addresses.
  2. Octal: Base-8 is used for Unix file permissions (chmod 755).
  3. Base-36: Some URL shorteners and tracking systems use Base-36 to encode long IDs into short, alphanumeric codes.
  4. Binary: Base-2 for subnet masks, bitwise flags, and low-level debugging.

Important Considerations

32-Bit Limit

The input decimal number is limited to signed 32-bit integers (max 2,147,483,647). For positive-only conversions this is the hard ceiling. When handling negative numbers, set /a "num=0 - num" overflows for the minimum value of -2,147,483,648. In practice, inputs should stay within ±2,147,483,647.

Lowercase Output

The script outputs uppercase letters (A–Z) for digits above 9. If your system requires lowercase hex or base-36, pipe the result through a PowerShell conversion:

for /f "usebackq delims=" %%L in (`powershell -NoProfile -Command "'!result!'.ToLower()"`) do set "result=%%L"
Reverse Conversion

To convert from an arbitrary base back to decimal, use a complementary "Power Accumulator" algorithm that multiplies each digit's value by base^position and sums the results. Batch's set /a natively supports 0x prefix for hex-to-decimal and 0 prefix for octal-to-decimal conversions.

Conclusion

Building a universal base converter is one of the most versatile mathematical tools you can add to your scripting toolkit. By mastering the "Divide and Collect Remainders" methodology, you create a single algorithm that handles Binary, Octal, Hex, and any other base with equal ease. This flexibility is invaluable for network analysis, data encoding, and low-level system debugging.