UTF8编码更改数据格式 [英] UTF8 Encoding changes data Format

查看:58
本文介绍了UTF8编码更改数据格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在PowerShell中获取命令的输出并对其进行编码,然后再次对其进行解码,以接收所述命令的结果,如图所示.

I'm trying to get the output of a command in PowerShell and encode it and then decode it again to receive the results of the said command as shown.

$enc = [system.Text.Encoding]::UTF8
$bytes = $enc.GetBytes((Invoke-Expression "net users"))
$enc.GetString($bytes)

但是,与原始的 net users 命令相反,结果格式不正确.我尝试将编码更改为ASCII和Unicode,但结果仍然格式错误.

However, the result comes out malformed as opposed to the original net users command. I've tried changing the encodings to ASCII and Unicode and still the result is malformed.

关于如何保持格式的任何想法?

Any ideas on how to maintain the formatting?

推荐答案

问题不是由编码引起的,而是因为PowerShell会破坏命令输出,除非您将其强制为字符串:

The problem isn't caused by the encoding, but because PowerShell will mangle the command output unless you force it into a string:

$bytes = $enc.GetBytes((Invoke-Expression "net users" | Out-String))

您不需要 Invoke-Expression BTW.这也将起作用:

You don't need Invoke-Expression BTW. This will work as well:

$bytes = $enc.GetBytes((net users | Out-String))

这篇关于UTF8编码更改数据格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆