Home » PowerShell Tips » PowerShell – Convert Decimal to Hex

PowerShell – Convert Decimal to Hex

To convert decimal to Hex in the PowerShell app, use ‘{0:x}’ -f to format strings by using the format method of string objects and decimal numbers. It will convert decimal to hex string in PowerShell.

PowerShell - Convert Decimal to Hex String
PowerShell – Convert Decimal to Hex String

In this article, let’s understand conversion from decimal to base 16 or convert hex to decimal in PowerShell with examples.

PowerShell – Convert Decimal to Hex String

Use string format ‘{0:x}’ or ‘{0:X}’ -f and decimal number to convert decimal to hexadecimal string.

PS C:\> '{0:X}' -f 1023                                                                                                 
3FF
PS C:\> '{0:X}' -f 171                                                                                                  
AB
PS C:\>                                                                                                                            

In the above PowerShell script, the 1023 decimal number is converted to base 16 3FF.

PowerShell Tip: How to use multiline commands in PowerShell!

PowerShell – Convert Decimal to Hex using .Net String

Using System.Convert .Net Framework class to convert decimal to hexadecimal string in PowerShell.

Or Use [System.String] .Net Framework class to convert decimal to base 16,

PS C:\> [System.Convert]::ToString(2021,16)            
7e5
PS C:\> [System.Convert]::ToString(1021,16)                                                                             
3fd
PS C:\> [System.Convert]::ToString(4031,16)                                                                             
fbf

The above PowerShell script converts decimal to base 16 format.

PowerShell Tip: How to fix the PowerShell script is not digitally signed error in PowerShell!

Conclusion

Using System.Convert or System.String .Net Framework class to convert decimal to hexadecimal string in PowerShell.

PowerShell Tip: How to use an empty recycle bin in PowerShell!

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.