powershell 转义 unicode (utf8)
powershell to unescape unicode (utf8)
我准备了以下函数
function UnescapeNonIsoChar($inputString) {
return [regex]::replace($inputString, '(?:\u[0-9a-f]{4})+', {
param($m)
$utf8Bytes = (-split ($m.Value -replace '\u([0-9a-f]{4})', '0x ')).ForEach([byte])
[text.encoding]::utf8.GetString($utf8Bytes)
})
}
一切正常,直到我得到 2019 \u2019
或比 \u0 值更大的东西(这里有任何 3 个值 [0-f])
然后它抛出错误:
Cannot convert value "0x2019" to type "System.Byte"
有人可以帮我吗?
编辑(添加输入)
profile.header.profile=\u00e6\u00aa\u0094\u00e6\u00a1\u0088\u00e5\u0090\u008d\u00e7\u00a8\u00b1
profile.header.customer=\u00e5\u00ae\u00a2\u00e6\u0088\u00b6\u00e5\u0090\u008d\u00e7\u00a8\u00b1
profile.header.account=\u00e5\u00b8\u00b3\u00e8\u0099\u009f/\u00e6\u00a2\u009d\u00e4\u00bb\u00b6\u00e4\u00bb\u00a3\u00e7\u00a2\u00bc
profile.header.description=\u00e6\u008f\u008f\u00e8\u00bf\u00b0
layout.msg.updatePrimaryUsersLayout=Kindly save it as a New Layout as Primary user\u2019s layout cannot be updated.
这是我收到的东西。重点是将所有转义字符转换为可读形式。这是类似于翻译文件的 stg。但对于应用程序而言是可读的,而不是对于用户而言。我需要一步将所有字符转义为可读形式。因此用户可以阅读或更改它。然后我需要将它转义回来,以便它可用于应用程序。
谢谢
使用您的样本输入:
function UnescapeNonIsoChar($inputString) {
Try {
[regex]::replace($inputString, '(?:\u[0-9a-f]{4})+', {
param($m)
$utf8Bytes = (-split ($m.Value -replace '\u([0-9a-f]{4})', '0x ')).ForEach([byte])
[text.encoding]::utf8.GetString($utf8Bytes)
})
} Catch {
[regex]::Unescape($inputString)
}
}
@'
profile.header.profile=\u00e6\u00aa\u0094\u00e6\u00a1\u0088\u00e5\u0090\u008d\u00e7\u00a8\u00b1
profile.header.customer=\u00e5\u00ae\u00a2\u00e6\u0088\u00b6\u00e5\u0090\u008d\u00e7\u00a8\u00b1
profile.header.account=\u00e5\u00b8\u00b3\u00e8\u0099\u009f/\u00e6\u00a2\u009d\u00e4\u00bb\u00b6\u00e4\u00bb\u00a3\u00e7\u00a2\u00bc
profile.header.description=\u00e6\u008f\u008f\u00e8\u00bf\u00b0
layout.msg.updatePrimaryUsersLayout=Kindly save it as a New Layout as Primary user\u2019s layout cannot be updated.
'@ -split [System.Environment]::NewLine |
ForEach-Object {
UnescapeNonIsoChar -inputString $_
}
输出:.\SO679444.ps1
profile.header.profile=檔案名稱
profile.header.customer=客戶名稱
profile.header.account=帳號/條件代碼
profile.header.description=描述
layout.msg.updatePrimaryUsersLayout=Kindly save it as a New Layout as Primary user’s layout cannot be updated.
编辑。 ...帮我换个方式?那个 unescapet 转义形式?.
您可以使用以下代码片段:
$Readable = .\SO679444.ps1
Import-Namespace -Namespace 'System.Web'
foreach ($line in $Readable) {
([char[]]$line | ForEach-Object {
if ([int]$_ -le 0xFF) { $_ } else {
[System.Web.HttpUtility]::UrlEncode([string]$_) -replace '%', '\u00'
}
}) -join ''
}
profile.header.profile=\u00e6\u00aa\u0094\u00e6\u00a1\u0088\u00e5\u0090\u008d\u00e7\u00a8\u00b1
profile.header.customer=\u00e5\u00ae\u00a2\u00e6\u0088\u00b6\u00e5\u0090\u008d\u00e7\u00a8\u00b1
profile.header.account=\u00e5\u00b8\u00b3\u00e8\u0099\u009f/\u00e6\u00a2\u009d\u00e4\u00bb\u00b6\u00e4\u00bb\u00a3\u00e7\u00a2\u00bc
profile.header.description=\u00e6\u008f\u008f\u00e8\u00bf\u00b0
layout.msg.updatePrimaryUsersLayout=Kindly save it as a New Layout as Primary user\u00e2\u0080\u0099s layout cannot be updated.
(也许对以 profile.header
字符串开头的行 not 有条件地使用另一个转换?)
我准备了以下函数
function UnescapeNonIsoChar($inputString) {
return [regex]::replace($inputString, '(?:\u[0-9a-f]{4})+', {
param($m)
$utf8Bytes = (-split ($m.Value -replace '\u([0-9a-f]{4})', '0x ')).ForEach([byte])
[text.encoding]::utf8.GetString($utf8Bytes)
})
}
一切正常,直到我得到 2019 \u2019
或比 \u0 值更大的东西(这里有任何 3 个值 [0-f])
然后它抛出错误:
Cannot convert value "0x2019" to type "System.Byte"
有人可以帮我吗?
编辑(添加输入)
profile.header.profile=\u00e6\u00aa\u0094\u00e6\u00a1\u0088\u00e5\u0090\u008d\u00e7\u00a8\u00b1
profile.header.customer=\u00e5\u00ae\u00a2\u00e6\u0088\u00b6\u00e5\u0090\u008d\u00e7\u00a8\u00b1
profile.header.account=\u00e5\u00b8\u00b3\u00e8\u0099\u009f/\u00e6\u00a2\u009d\u00e4\u00bb\u00b6\u00e4\u00bb\u00a3\u00e7\u00a2\u00bc
profile.header.description=\u00e6\u008f\u008f\u00e8\u00bf\u00b0
layout.msg.updatePrimaryUsersLayout=Kindly save it as a New Layout as Primary user\u2019s layout cannot be updated.
这是我收到的东西。重点是将所有转义字符转换为可读形式。这是类似于翻译文件的 stg。但对于应用程序而言是可读的,而不是对于用户而言。我需要一步将所有字符转义为可读形式。因此用户可以阅读或更改它。然后我需要将它转义回来,以便它可用于应用程序。
谢谢
使用您的样本输入:
function UnescapeNonIsoChar($inputString) {
Try {
[regex]::replace($inputString, '(?:\u[0-9a-f]{4})+', {
param($m)
$utf8Bytes = (-split ($m.Value -replace '\u([0-9a-f]{4})', '0x ')).ForEach([byte])
[text.encoding]::utf8.GetString($utf8Bytes)
})
} Catch {
[regex]::Unescape($inputString)
}
}
@'
profile.header.profile=\u00e6\u00aa\u0094\u00e6\u00a1\u0088\u00e5\u0090\u008d\u00e7\u00a8\u00b1
profile.header.customer=\u00e5\u00ae\u00a2\u00e6\u0088\u00b6\u00e5\u0090\u008d\u00e7\u00a8\u00b1
profile.header.account=\u00e5\u00b8\u00b3\u00e8\u0099\u009f/\u00e6\u00a2\u009d\u00e4\u00bb\u00b6\u00e4\u00bb\u00a3\u00e7\u00a2\u00bc
profile.header.description=\u00e6\u008f\u008f\u00e8\u00bf\u00b0
layout.msg.updatePrimaryUsersLayout=Kindly save it as a New Layout as Primary user\u2019s layout cannot be updated.
'@ -split [System.Environment]::NewLine |
ForEach-Object {
UnescapeNonIsoChar -inputString $_
}
输出:.\SO679444.ps1
profile.header.profile=檔案名稱 profile.header.customer=客戶名稱 profile.header.account=帳號/條件代碼 profile.header.description=描述 layout.msg.updatePrimaryUsersLayout=Kindly save it as a New Layout as Primary user’s layout cannot be updated.
编辑。 ...帮我换个方式?那个 unescapet 转义形式?.
您可以使用以下代码片段:
$Readable = .\SO679444.ps1
Import-Namespace -Namespace 'System.Web'
foreach ($line in $Readable) {
([char[]]$line | ForEach-Object {
if ([int]$_ -le 0xFF) { $_ } else {
[System.Web.HttpUtility]::UrlEncode([string]$_) -replace '%', '\u00'
}
}) -join ''
}
profile.header.profile=\u00e6\u00aa\u0094\u00e6\u00a1\u0088\u00e5\u0090\u008d\u00e7\u00a8\u00b1 profile.header.customer=\u00e5\u00ae\u00a2\u00e6\u0088\u00b6\u00e5\u0090\u008d\u00e7\u00a8\u00b1 profile.header.account=\u00e5\u00b8\u00b3\u00e8\u0099\u009f/\u00e6\u00a2\u009d\u00e4\u00bb\u00b6\u00e4\u00bb\u00a3\u00e7\u00a2\u00bc profile.header.description=\u00e6\u008f\u008f\u00e8\u00bf\u00b0 layout.msg.updatePrimaryUsersLayout=Kindly save it as a New Layout as Primary user\u00e2\u0080\u0099s layout cannot be updated.
(也许对以 profile.header
字符串开头的行 not 有条件地使用另一个转换?)