(view source code of chr.ps as plain text)
param (
[parameter( Mandatory = $true, HelpMessage = "Enter an integer between 0 and 255", ValueFromPipeline = $true )]
[ValidateScript({
try {
# Check if an integer was specified
if ( [int32]::TryParse( $_, [ref]0 ) ) {
# Check if the integer is within the allowed range
if ( ( [int]$_ -ge 0 ) -and ( [int]$_ -le 255 ) ) {
# Return just the result, without newline
Write-Host ( [char][int]$_ ) -NoNewline
}
}
}
catch { }
# Always return true so we can use our custom error handling
$true
})]
[object]$charnum,
[parameter(ValueFromRemainingArguments=$true)]
[object]$ignored
)
# Repeat the test of the input, so we can show the appropriate error message
$rc = 1
if ( $charnum -ne "/?" ) {
if ( [int32]::TryParse( $charnum, [ref]0 ) ) {
if ( ( [int]$charnum -ge 0 ) -and ( [int]$charnum -le 255 ) ) {
$rc = 0
} else {
Write-Host "`nError: " -ForegroundColor Red -NoNewline
Write-Host "Specified integer should be in range 0..255`n" -ForegroundColor White
}
} else {
Write-Host "`nError: " -ForegroundColor Red -NoNewline
Write-Host "Please specify an " -NoNewline
Write-Host "integer " -ForegroundColor White -NoNewline
Write-Host "between 0 and 255`n"
}
}
if ( $rc -ne 0 ) {
Write-Host
Write-Host "Chr.ps1, Version 1.01"
Write-Host "Returns the ASCII character for the specified numeric value"
Write-Host
Write-Host "Usage: " -NoNewline
Write-Host "CHR.PS1 number" -ForegroundColor White
Write-Host
Write-Host "Returns: ASCII character for specified " -NoNewline
Write-Host "number" -ForegroundColor White -NoNewline
Write-Host " (0..255)"
Write-Host
Write-Host "Notes: The ASCII character is written to Standard Output (i.e. the screen)."
Write-Host " The script will prompt for input if command line argument is missing."
Write-Host " Piped input is accepted; if multiple arguments are piped, all will"
Write-Host " be handled; if multiple arguments are passed on the command line,"
Write-Host " however, the script will ignore all but the first argument."
Write-Host " The `"errorlevel`" is 1 in case of errors, otherwise 0."
Write-Host
Write-Host "Written by Rob van der Woude"
Write-Host "http://www.robvanderwoude.com"
}
Exit $rc
page last modified: 2024-04-16; loaded in 0.0097 seconds