【问题标题】:Encode a string in UTF-8以 UTF-8 编码字符串
【发布时间】:2014-06-09 00:44:14
【问题描述】:

我想在 PowerShell 中将字符串编码为 UTF8。

这是我尝试过的:

$consumer_key ="xvz1evFS4wEEPTGEFPHBog"
$enc_consumer_key = System.Text.UTF8Encoding($consumer_key)

但我得到一个错误:

System.Text.UTF8Encoding 无法识别为 cmdlet 的名称

【问题讨论】:

    标签: powershell utf-8 encode


    【解决方案1】:

    试试这个:

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

    【讨论】:

      【解决方案2】:

      如果您只想将字符串写入文件:

      $consumer_key ="xvz1evFS4wEEPTGEFPHBog"
      $consumer_key |  Out-File c:\path\utf8file.txt -Encoding UTF8
      

      【讨论】:

        【解决方案3】:

        编码/解码:

        $enc = [System.Text.Encoding]::UTF8.GetBytes("â")
        # 195 162
        [System.Text.Encoding]::UTF8.GetString($enc)
        # â
        [System.Text.Encoding]::ASCII.GetString($enc)
        # ??
        [System.Text.Encoding]::Default.GetString($enc) # Windows-1252
        # â
        

        这是我搜索的最佳问题,它引导我找到上述 PowerShell 中文本编码/解码字符的解决方案。就我而言,我试图调试格式错误的 UTF8 字符。希望它对未来的人有所帮助。

        -检查BOM

        【讨论】:

          猜你喜欢
          • 2013-06-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多