'Hashtable to base64 conversion
I'm looking for a way out for converting hash table to base64 in PowerShell. Not sure that's possible or not. Can anyone help me on this?
Strings can be converted to base64 via byte. I'm seeing only string conversion examples.
Please help me to figure this out to proceed further.
Thanks.
Solution 1:[1]
Convert the hash to JSON. Convert the JSON to Base64.
$hash = @{key = "value"}
# encode
$json = $hash | ConvertTo-JSON -Depth 100 -Compress
$bytes = [System.Text.Encoding]::UTF8.GetBytes($json)
$b64 = [System.Convert]::ToBase64String($bytes) # => 'eyJrZXkiOiJ2YWx1ZSJ9'
# decode
$bytes = [System.Convert]::FromBase64String($b64)
$json = [System.Text.Encoding]::UTF8.GetString($bytes)
$hash = $json | ConvertFrom-Json
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tomalak |
