3

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
GSerg
  • 76,472
  • 17
  • 159
  • 346
mrinali
  • 140
  • 10
  • 1
    [􀂇](https://www.compart.com/en/unicode/U+100087) 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`. – GSerg Jul 23 '19 at 07:26
  • Is there any way to convert it back to its original form? – mrinali Jul 23 '19 at 08:53
  • [`HtmlDecode("􀂇")`](https://learn.microsoft.com/en-us/dotnet/api/system.web.httputility.htmldecode?view=netframework-4.8#System_Web_HttpUtility_HtmlDecode_System_String_)? – GSerg Jul 23 '19 at 08:55
  • 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.https://stackoverflow.com/questions/6115708/classic-asp-vbscript-convert-html-codes-to-plain-text .This function work fine but gives error if the value exceeds "65635" because to ChrW(). – mrinali Jul 23 '19 at 09:03
  • 1
    Are you using classic ASP and vbscript, as opposed to ASP.NET and VB.NET with which you tagged the question? – GSerg Jul 23 '19 at 09:04
  • 1
    Classic Asp. sorry for the inconvenience. i have edited it – mrinali Jul 23 '19 at 09:07

1 Answers1

2
' 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
GSerg
  • 76,472
  • 17
  • 159
  • 346