Skip to content

Instantly share code, notes, and snippets.

@gpduck
Last active December 21, 2015 12:49
Show Gist options
  • Save gpduck/6308785 to your computer and use it in GitHub Desktop.
Save gpduck/6308785 to your computer and use it in GitHub Desktop.
Returns the hex value of any character using the specified encoding (default is Unicode).
function Get-HexValue {
param(
[Parameter(Mandatory=$true)]
[Char]$Character,
[ValidateSet("ASCII","BigEndianUnicode","Default","Unicode","UTF32","UTF7","UTF8")]
[String]$Encoding = "Unicode"
)
$E = [Text.Encoding]::$Encoding
$Bytes = $E.GetBytes($Character)
if($Encoding -ne "BigEndianUnicode") {
[Array]::Reverse($Bytes)
}
-join ( $Bytes | Foreach-Object { "{0:X2}" -f $_ })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment