How to Convert a String to a Binary Number in PowerShell?

Recently, I was required to convert a string to a binary number in PowerShell. I tried different methods. In this tutorial, I will show you how to convert a string to a binary number in PowerShell using different methods with examples.

To convert a string to a binary number in PowerShell, you can use a custom function that iterates through each character, converting it to its binary representation using [Convert]::ToString([byte][char]$char, 2).PadLeft(8, '0').

Binary Numbers in PowerShell

Binary numbers are the foundation of all computing systems. They are composed of only two digits: 0 and 1. Each binary digit represents a power of 2. For instance, the binary number 101 represents (1 \times 2^2 + 0 \times 2^1 + 1 \times 2^0 = 4 + 0 + 1 = 5) in decimal.

Convert a String to a Binary Number in PowerShell

There are different methods to convert a string to a binary number in PowerShell with examples.

Method 1: Using Custom PowerShell Function

One of the simplest ways to convert a string to a binary number in PowerShell is by writing a custom function. Let me show you an example.

Example

Here is the complete example and the PowerShell script.

function Convert-StringToBinary {
    param (
        [string]$inputString
    )

    $binaryResult = ""
    foreach ($char in $inputString.ToCharArray()) {
        $binaryChar = [Convert]::ToString([byte][char]$char, 2).PadLeft(8, '0')
        $binaryResult += $binaryChar + " "
    }
    return $binaryResult.Trim()
}

# Usage
$string = "Hello"
$binaryString = Convert-StringToBinary -inputString $string
Write-Output $binaryString

In this example, the Convert-StringToBinary function takes a string as input and converts each character to its binary representation. The ToString method with base 2 is used to convert each character to binary, and PadLeft ensures each binary number is 8 bits long.

You can see the exact output in the screenshot below after I executed the above PowerShell script.

Convert a String to a Binary Number in PowerShell

Method 2: Using PowerShell’s Built-in Conversion Capabilities

PowerShell provides built-in capabilities to handle different data types and conversions. You can leverage these to convert strings to binary numbers in PowerShell.

Example

Here is an example.

$string = "Hello"
$binaryArray = @()

foreach ($char in $string.ToCharArray()) {
    $binaryArray += [Convert]::ToString([byte][char]$char, 2).PadLeft(8, '0')
}

$binaryString = $binaryArray -join " "
Write-Output $binaryString

This method is similar to the first one but uses an array to store the binary representations of each character. The -join operator is then used to concatenate the array elements into a single string.

You can see the output in the screenshot below; after I executed the above PowerShell script.

PowerShell Convert a String to a Binary Number

Method 3: Using .NET Classes

PowerShell is built on the .NET framework, which means you can use .NET classes and methods directly within your scripts. This can be particularly powerful for more complex conversions and manipulations.

Example

Here is the complete PowerShell script.

Add-Type -TypeDefinition @"
public class BinaryConverter {
    public static string StringToBinary(string data) {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        foreach (char c in data.ToCharArray()) {
            sb.Append(System.Convert.ToString(c, 2).PadLeft(8, '0') + " ");
        }
        return sb.ToString().Trim();
    }
}
"@

# Usage
$string = "Hello"
$binaryString = [BinaryConverter]::StringToBinary($string)
Write-Output $binaryString

In this example, we define a new .NET class BinaryConverter using the Add-Type cmdlet. This class has a static method StringToBinary that converts a string to its binary representation.

Conclusion

In this tutorial, I have explained how to convert a string to a binary number in PowerShell using different methods.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.