Alternative of ChrW function
Asked Answered
F

1

4

Is there any alternative function/solution of the ChrW() which accepts value not in range is -32768–65535 like for character code 􀂇 which leads to "􀂇". Using ChrW() gives error

"Invalid procedure call or argument"

So I want an alternative solution to convert the charactercode to the actual character.

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
Fugacious answered 23/7, 2019 at 7:18 Comment(6)
&#1048711 is two UTF-16 characters, not one. Hence it cannot be produced with a single call to ChrW or any other function that returns a Char.Kentonkentucky
Is there any way to convert it back to its original form?Fugacious
HtmlDecode("􀂇")?Kentonkentucky
I want to do it in .asp page.It doesn't have built in function so i have created function similar in attached link by C Ross link.#6116208 .This function work fine but gives error if the value exceeds "65635" because to ChrW().Fugacious
Are you using classic ASP and vbscript, as opposed to ASP.NET and VB.NET with which you tagged the question?Kentonkentucky
Classic Asp. sorry for the inconvenience. i have edited itFugacious
K
3
' 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
Kentonkentucky answered 23/7, 2019 at 9:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.