How to Convert Binary String to Decimal in PowerShell
Introduction
Converting binary string to decimal can be a tricky process. Fortunately, PowerShell provides an easy way to convert binary string into decimal number of integer type.
In this blog post, we’ll take a look at how you can use some commands to convert binary string into decimal.
Solution
Using Convert class from .NET Framework
.NET Framework classes provide powerful methods for performing various types of data conversion tasks including converting string into decimal number which is of integer type.
The most commonly used class is System.Convert
which provides several static methods that are specifically designed for converting string into different data types such as int, double, etc.
To get list of static methods that can be used for conversion, we can use following command:
[Convert] | Get-Member -Static
Later, to convert binary number in form of string to decimal, we can use following script:
$string = "01000111110101"
$num = [Convert]::ToInt32($string, 2)
Write-Host $num
The output of above script is 4597.
Conclusion
Using PowerShell commands makes converting binary string to decimal number incredibly easy and efficient, just remember we can use ToInt32
static method from System.Convert
class to convert the binary string.