LLI Wiki — My Digital Knowledge Base

PowerShell: convert a string to a byte array (UTF-8)

Get the bytes of a string with [System.Text.Encoding]::UTF8.GetBytes().

To get the byte array of a string in PowerShell (for example to compute a signature or a hash), use the .NET System.Text.Encoding class.

$enc = [System.Text.Encoding]::UTF8
$consumerkey = "<CONSUMER_KEY>"
$encconsumerkey = $enc.GetBytes($consumerkey)

GetBytes() returns a byte[]: the UTF-8 encoding of the string. For another encoding, replace UTF8 with ASCII, Unicode (UTF-16), etc. The key is a placeholder here; never store a real key in a script.

Mastodon LLIMastodon SocialFosstodon