utf-8 - ASP : I can´t decode some character from utf-8 to iso-8859-1

标签 utf-8 asp-classic utf8-decode

我使用这个函数来解码 UTF-8:

function DecodeUTF8(s)
  dim i
  dim c
  dim n
  i = 1
  do while i <= len(s)
    c = asc(mid(s,i,1))
    if c and &H80 then
      n = 1
      do while i + n < len(s)
        if (asc(mid(s,i+n,1)) and &HC0) <> &H80 then
          exit do
        end if
        n = n + 1
      loop
      if n = 2 and ((c and &HE0) = &HC0) then
        c = asc(mid(s,i+1,1)) + &H40 * (c and &H01)
      else
        c = 191 
      end if
      s = left(s,i-1) + chr(c) + mid(s,i+n)
    end if
    i = i + 1
  loop

  DecodeUTF8 = s
end function

但是解码这些字符有一些问题:

€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ

在那种情况下

c=191-->c='¿'

我找到了一些与此问题相关的信息: http://www.i18nqa.com/debug/utf8-debug.html

你知道什么函数可以正确解码吗?

最佳答案

Public Function DecodeUTF8(s)
  Set stmANSI = Server.CreateObject("ADODB.Stream")
  s = s & ""
  On Error Resume Next

  With stmANSI
    .Open
    .Position = 0
    .CharSet = "Windows-1252"
    .WriteText s
    .Position = 0
    .CharSet = "UTF-8"
  End With

  DecodeUTF8 = stmANSI.ReadText
  stmANSI.Close

  If Err.number <> 0 Then
    lib.logger.error "str.DecodeUTF8( " & s & " ): " & Err.Description
    DecodeUTF8 = s
  End If
  On error Goto 0
End Function

关于utf-8 - ASP : I can´t decode some character from utf-8 to iso-8859-1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21751893/

相关文章:

php - php mysql 字符编码

java - ANSI 到 UTF-8 通过 Java : some lines are lost

com - 我怎样才能找到更多关于我的经典 ASP 环境的信息?

用于密码验证的正则表达式 (ASP)

数据网格和经典 ASP

java - 将转义的 Unicode 字符转换回实际字符

php - UTF-8贯穿始终

mysql - utf8 mysql字符串音译

java - android & Java 中的外来字符

c# - UTF-8 在 C# 中用双引号编码字符串