Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for chr.ps

(view source code of chr.ps as plain text)

  1. param (
  2.     [parameter( Mandatory = $true, HelpMessage = "Enter an integer between 0 and 255", ValueFromPipeline = $true )]
  3.     [ValidateScript({
  4.         try {
  5.             # Check if an integer was specified
  6.             if ( [int32]::TryParse( $_, [ref]0 ) ) {
  7.                 # Check if the integer is within the allowed range
  8.                 if ( ( [int]$_ -ge 0 ) -and ( [int]$_ -le 255 ) ) {
  9.                     # Return just the result, without newline
  10.                     Write-Host ( [char][int]$_ ) -NoNewline
  11.                 }
  12.             }
  13.         }
  14.         catch { }
  15.         # Always return true so we can use our custom error handling
  16.         $true
  17.     })]
  18.     [object]$charnum,
  19.     [parameter(ValueFromRemainingArguments=$true)]
  20.     [object]$ignored
  21. )
  22.  
  23.  
  24. # Repeat the test of the input, so we can show the appropriate error message
  25. $rc = 1
  26. if ( $charnum -ne "/?" ) {
  27.     if ( [int32]::TryParse( $charnum, [ref]0 ) ) {
  28.         if ( ( [int]$charnum -ge 0 ) -and ( [int]$charnum -le 255 ) ) {
  29.             $rc = 0
  30.         } else {
  31.             Write-Host "`nError: " -ForegroundColor Red -NoNewline
  32.             Write-Host "Specified integer should be in range 0..255`n" -ForegroundColor White
  33.         }
  34.     } else {
  35.         Write-Host "`nError: " -ForegroundColor Red -NoNewline
  36.         Write-Host "Please specify an " -NoNewline
  37.         Write-Host "integer " -ForegroundColor White -NoNewline
  38.         Write-Host "between 0 and 255`n"
  39.     }
  40. }
  41.  
  42. if ( $rc -ne 0 ) {
  43.     Write-Host
  44.     Write-Host "Chr.ps1,  Version 1.01"
  45.     Write-Host "Returns the ASCII character for the specified numeric value"
  46.     Write-Host
  47.     Write-Host "Usage:   " -NoNewline
  48.     Write-Host "CHR.PS1  number" -ForegroundColor White
  49.     Write-Host
  50.     Write-Host "Returns: ASCII character for specified " -NoNewline
  51.     Write-Host "number" -ForegroundColor White -NoNewline
  52.     Write-Host " (0..255)"
  53.     Write-Host
  54.     Write-Host "Notes:   The ASCII character is written to Standard Output (i.e. the screen)."
  55.     Write-Host "         The script will prompt for input if command line argument is missing."
  56.     Write-Host "         Piped input is accepted; if multiple arguments are piped, all will"
  57.     Write-Host "         be handled; if multiple arguments are passed on the command line,"
  58.     Write-Host "         however, the script will ignore all but the first argument."
  59.     Write-Host "         The `"errorlevel`" is 1 in case of errors, otherwise 0."
  60.     Write-Host
  61.     Write-Host "Written by Rob van der Woude"
  62.     Write-Host "http://www.robvanderwoude.com"
  63. }
  64.  
  65. Exit $rc

page last uploaded: 2017-04-06, 13:33