Home » PowerShell Tips » PowerShell – Convert Decimal to Hex

PowerShell – Convert Decimal to Hex

To convert decimal to Hex in PowerShell app, use ‘{0:x} -f to format strings by using the format method of string objects and decimal number. 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, 1023 decimal number converted to base 16 3FF

PowerShell Tip: How to use multiline command 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

In the above PowerShell script, it convert decimal to base 16 format.

PowerShell Tip: How to fix 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 empty recycle bin in PowerShell!

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

Leave a Comment