# Get-ParsedString2.ps1
# MSDN Sample recoded using PowerShell
# See: http://msdn.microsoft.com/en-us/library/system.globalization.numberstyles(VS.85).aspx
# This script usesd the .value__ to obtain the value of each enum
# Thomas Lee - tfl@psp.co.uk
# Parse the string as a hex value and display the value as a decimal.
$num = "A"
$val = [system.int32]::Parse($num, [System.Globalization.NumberStyles]::HexNumber)
"{0} in hex = {1} in decimal" -f $num,$val
# Parse the string, allowing a leading sign, and ignoring leading and trailing white spaces.
$num = " -45 "
# create parsing value using value__
$parsing = [System.Globalization.NumberStyles]::AllowLeadingSign.value__+
[System.Globalization.NumberStyles]::AllowLeadingWhite.value__ +
[System.Globalization.NumberStyles]::AllowTrailingWhite.value__
# parse and display
$val = [system.int32]::Parse($num,$parsing)
"'{0}' parsed to an int32 is '{1}'." -f $num, $val
# Now try a negative number
$num = " (37) "
$parsing = [System.Globalization.NumberStyles]::AllowParentheses.value__ +
[System.Globalization.NumberStyles]::AllowLeadingSign.value__ +
[System.Globalization.NumberStyles]::AllowLeadingWhite.value__ +
[System.Globalization.NumberStyles]::AllowTrailingWhite.value__
# And parse/display it
$val = [system.int32]::Parse($num, $parsing)
"'{0}' parsed to an int32 is '{1}'." -f $num, $val
This script produces the following output:
PSH [D:\foo]: .\Get-ParsedString2.ps1'
A in hex = 10 in decimal
' -45 ' parsed to an int32 is '-45'.
' (37) ' parsed to an int32 is '-37'.