How to Convert Hexadecimal String to Integer in PowerShell

Introduction

Converting hexadecimal string to integer type can be a tricky process. Fortunately, PowerShell provides an easy way to convert hexadecimal string into integer and other data types.

In this blog post, we’ll take a look at how you can use some commands to convert hexadecimal string into integer.

Solution

Using Casting Operator

This method is the most straightforward way to convert string to integer. This method works just like casting in programming languages such as C#, Java, etc.

However, there is an exception when the string is in hexadecimal format. The string value must be prepended with 0x before being casted as in following example:


$string = "ADFE"
$num = [Int32]"0x$string"

Write-Host $num

The output of above script is 44542.

Using Convert class from .NET Framework

The second way that we will discuss is by utilizing the .NET Framework classes provided by Microsoft. These classes provide powerful methods for performing various types of data conversion tasks including converting string into integer.

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 hexadecimal number in form of string to integer, we can use following script:


$string = "ADFE"
$num = [Convert]::ToInt32($string, 16)

Write-Host $num

Using Parse Method from .NET Framework Int32 Classes

Another .NET Framework class that can be used is Int32 struct. It provides method like Parse to convert string to integer.

Specifically we must pass the style of numeric string. In this case, the string is in hexadecimal format.


$string = "ADFE"
$num = [Int32]::Parse($string, [System.Globalization.NumberStyles]::HexNumber )

Write-Host $num

Using TryParse Method from .NET Framework Int32 Classes

Besides Parse method, Int32 struct also has a safe method to convert string to integer which is TryParse. This method will check whether the string can be converted to integer or not while at the same time it will out the conversion result.

To do this, we must use ref parameter modifer which is the counterpart of out keyword in C#. Similar to previous method, we must specify the numeric style but the difference is we must also specify the culture.


using namespace System.Globalization

$string = "ADFE"
$num = $null
$success = [Int32]::TryParse($string, [NumberStyles]::HexNumber, [CultureInfo]::InvariantCulture, [ref]$num)

if ($success) {
    Write-Host $num
}

Conclusion

In conclusion, there are several ways to easily convert hexadecimal string into integer in PowerShell.

While all methods produce the same result, the last method which is using TryParse supposed to be the safest way to convert hexadecimal string to integer. It’s because sometimes we do not know whether the string supplied representing a valid hexadecimal number or not.