- <#
- .SYNOPSIS
- This script implements an MSDN Encoding example in PowerShell
- .DESCRIPTION
- This script creates an encoding plus a Unicode string then
- manipulates it as per the C# sample.
- .NOTES
- File Name : Get-UTF8Encoding
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/05/get-utf8encoding.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.text.utf8encoding.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-UTF8Encoding.ps1
- Original string:This unicode string contains two characters with
- codes outside an 8-bit code range, Pi (π) and Sigma (Σ).
- Encoded bytes:
- <long table omitted>
- Decoded bytes:
- This unicode string contains two characters with codes outside an
- 8-bit code range, Pi (π) and Sigma (Σ).
- #>
- # Start of Script
- # Create a UTF-8 encoding.
- $utf8 = New-Object System.Text.utf8encoding
- $unicodeString =
- "This unicode string contains two characters " +
- "with codes outside an 8-bit code range, " +
- "Pi (" + [char] 0x03a0 + ") and Sigma (" + [char] 0x03a3 + ")."
- "Original string:{0}" -f $unicodeString
- # Encode the string.
- $encodedBytes = $utf8.GetBytes($unicodeString)
- ""
- "Encoded bytes:"
- foreach($b in $encodedBytes) {"[{0}]" -f $b}
- # Decode bytes back to string
- # Notice Pi and Sigma characters are still present
- $decodedString = $utf8.GetString($encodedBytes)
- "";"Decoded bytes:"
- $decodedString
- # End of Script
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Sunday, 2 May 2010
Get-UTF8Encoding
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment