0
0
PowershellConversionBeginner · 2 min read

PowerShell Script to Convert Decimal to Binary Number

Use the PowerShell expression [Convert]::ToString(decimalNumber, 2) to convert a decimal number to its binary string representation.
📋

Examples

Input0
Output0
Input10
Output1010
Input255
Output11111111
🧠

How to Think About It

To convert a decimal number to binary, think of dividing the number by 2 repeatedly and recording the remainder each time. These remainders, read in reverse order, form the binary number. PowerShell provides a built-in method to do this conversion easily by specifying base 2.
📐

Algorithm

1
Get the decimal number input.
2
Use a built-in method to convert the number to a binary string with base 2.
3
Return or print the binary string.
💻

Code

powershell
param([int]$decimalNumber)
$binaryString = [Convert]::ToString($decimalNumber, 2)
Write-Output $binaryString
Output
1010
🔍

Dry Run

Let's trace converting decimal 10 to binary using the script.

1

Input decimal number

decimalNumber = 10

2

Convert decimal to binary string

[Convert]::ToString(10, 2) returns '1010'

3

Output binary string

Print '1010'

DecimalBinary String
101010
💡

Why This Works

Step 1: Using built-in conversion

PowerShell's [Convert] class has a method ToString that converts numbers to strings in any base, here base 2 for binary.

Step 2: Passing base 2

Passing 2 as the second argument tells the method to convert the decimal number into binary format.

Step 3: Output as string

The method returns the binary number as a string, which can be printed or used further.

🔄

Alternative Approaches

Manual division and remainder
powershell
param([int]$decimalNumber)
$binary = ''
if ($decimalNumber -eq 0) { $binary = '0' }
while ($decimalNumber -gt 0) {
    $remainder = $decimalNumber % 2
    $binary = $remainder.ToString() + $binary
    $decimalNumber = [math]::Floor($decimalNumber / 2)
}
Write-Output $binary
This method shows the manual process but is longer and less efficient than using the built-in method.
Using ConvertTo-String with base parameter (PowerShell 7+)
powershell
param([int]$decimalNumber)
$binaryString = $decimalNumber.ToString(2)
Write-Output $binaryString
This uses the .NET ToString method on the integer directly, available in newer PowerShell versions.

Complexity: O(log n) time, O(log n) space

Time Complexity

Conversion depends on the number of bits in the decimal number, which is proportional to log base 2 of the number.

Space Complexity

The binary string length grows with the number of bits, so space is proportional to log base 2 of the input number.

Which Approach is Fastest?

Using the built-in [Convert]::ToString method is fastest and simplest compared to manual division.

ApproachTimeSpaceBest For
[Convert]::ToStringO(log n)O(log n)Quick and reliable conversion
Manual divisionO(log n)O(log n)Learning and understanding conversion process
.ToString(2) methodO(log n)O(log n)Modern PowerShell versions, concise code
💡
Use [Convert]::ToString(number, 2) for a quick and reliable decimal to binary conversion in PowerShell.
⚠️
Beginners often forget to specify base 2, which results in decimal output instead of binary.