经典 ASP 将拉丁字符转换为 Unicode 转义字符串

Classic ASP Convert Latin Characters to Unicode Escape Strings

我需要一个经典的 ASP 函数,它将接受一个字符串,例如 Jämshög 并将其转换为 J\u00e4msh\u00f6g,以便所有重音字符成为它们等效的 unicode 转义码。

我正在 JSON 字符串中将此数据发送到 API,它要求所有特殊字符都使用 unicode 转义码。

我似乎花了好几个小时才找到解决方案,但一直没找到。任何帮助将不胜感激。

看看下面 aspjson 中的函数。它还处理必须转义的非 unicode 字符,例如引号、制表符、换行符等。幸运的是没有依赖项,因此也可以独立工作。

Function jsEncode(str)
    Dim charmap(127), haystack()
    charmap(8)  = "\b"
    charmap(9)  = "\t"
    charmap(10) = "\n"
    charmap(12) = "\f"
    charmap(13) = "\r"
    charmap(34) = "\"""
    charmap(47) = "\/"
    charmap(92) = "\"

    Dim strlen : strlen = Len(str) - 1
    ReDim haystack(strlen)

    Dim i, charcode
    For i = 0 To strlen
        haystack(i) = Mid(str, i + 1, 1)

        charcode = AscW(haystack(i)) And 65535
        If charcode < 127 Then
            If Not IsEmpty(charmap(charcode)) Then
                haystack(i) = charmap(charcode)
            ElseIf charcode < 32 Then
                haystack(i) = "\u" & Right("000" & Hex(charcode), 4)
            End If
        Else
            haystack(i) = "\u" & Right("000" & Hex(charcode), 4)
        End If
    Next

    jsEncode = Join(haystack, "")
End Function