Powershell将变量转换为utf-16

Ant*_*ton 1 variables powershell utf-16

我正在努力导入一个 csv 文件,使所有内容成为变量,然后仅将其中一个变量转换为 utf-16。我还有其他一切都在努力将其转换为以下格式:

需要将单词转换Hello World为 的示例00480065006c006c006f00057006f0072006c0064

该脚本看起来与此类似。

$text = "Hello World"::UTF16

$text
Run Code Online (Sandbox Code Playgroud)

这可能吗?或者有什么方法可以转换吗?

The*_*heo 5

您想要的输出是 UTF-16 Big Endian 编码的十六进制表示形式,称为BigEndianUnicode

您可以创建一个辅助函数,将字符串转换为:

function ConvertTo-Utf16BE {
    [CmdletBinding()]
    Param (
        [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $true)]
        [string]$String
    )
    $enc = [system.Text.Encoding]::BigEndianUnicode
    ($enc.GetBytes($string) | ForEach-Object {'{0:X2}' -f $_ }) -join ''
}

'Hello World' | ConvertTo-Utf16BE
Run Code Online (Sandbox Code Playgroud)

输出:

00480065006C006C006F00200057006F0072006C0064