[英]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$1 ')).ForEach([byte])
[text.encoding]::utf8.GetString($utf8Bytes)
})
}
一切正常,直到我得到 2019 \’
或比 \\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 之類的翻譯文件。 但對應用程序可讀,而不是對用戶可讀。 我需要一步將所有字符轉義為可讀形式。 因此用戶可以閱讀它或更改它。 然后ofc我需要將它轉回以便它可用於應用程序。
謝謝
使用您的樣本輸入:
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$1 ')).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 $_
}
輸出: .\\SO\\62679444.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 = .\SO\62679444.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=\æ\ª\\æ\¡\\å\\\ç\¨\± 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.
(對於不以profile.header
字符串開頭的行,可能有條件地使用另一個轉換?)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.