【问题标题】:Alternative of ChrW functionChrW 功能的替代方案
【发布时间】:2019-12-01 04:14:31
【问题描述】:

ChrW() 是否有任何替代功能/解决方案接受不在 -32768–65535 范围内的值,例如字符代码 􀂇 导致“????”。使用ChrW() 会出错

“无效的过程调用或参数”

所以我想要一个替代解决方案来将字符代码转换为实际字符。

Code:

Function HTMLDecode(sText)
    Dim regEx
    Dim matches
    Dim match
    sText = Replace(sText, """, Chr(34))
    sText = Replace(sText, "<"  , Chr(60))
    sText = Replace(sText, ">"  , Chr(62))
    sText = Replace(sText, "&" , Chr(38))
    sText = Replace(sText, " ", Chr(32))

    Set regEx= New RegExp

    With regEx
     .Pattern = "&#(\d+);" 'Match html unicode escapes
     .Global = True
    End With

    Set matches = regEx.Execute(sText)

    'Iterate over matches
    For Each match In matches
        'For each unicode match, replace the whole match, with the ChrW of the digits.
        sText = Replace(sText, match.Value, ChrW(match.SubMatches(0)))
    Next

    HTMLDecode = sText
End Function

【问题讨论】:

  • &#1048711 是两个 UTF-16 字符,而不是一个。因此,它不能通过对ChrW 或任何其他返回Char 的函数的单个调用来生成。
  • 有什么办法可以将其转换回原来的形式?
  • 您使用的是经典的 ASP 和 vbscript,而不是您标记问题的 ASP.NET 和 VB.NET?
  • 经典 Asp。带来不便敬请谅解。我已经编辑了它

标签: unicode vbscript asp-classic unicode-escapes


【解决方案1】:
' https://en.wikipedia.org/wiki/UTF-16#Description
function Utf16Encode(byval unicode_code_point)
    if (unicode_code_point >= 0 and unicode_code_point <= &hD7FF&) or (unicode_code_point >= &hE000& and unicode_code_point <= &hFFFF&) Then
        Utf16Encode = ChrW(unicode_code_point)
    else    
        unicode_code_point = unicode_code_point - &h10000&
        Utf16Encode = ChrW(&hD800 Or (unicode_code_point \ &h400&)) & ChrW(&hDC00 Or (unicode_code_point And &h3FF&))
    end if
end function
For Each match In matches
    'For each unicode match, replace the whole match, with the ChrW of the digits.
    sText = Replace(sText, match.Value, Utf16Encode(CLng(match.SubMatches(0))))
Next

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多