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

一个人想着一个人 提交于 2019-12-02 08:32:26

问题


I use this function to 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

But there are some probles to decode that characters:

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

In that case

c=191-->c='¿'

I found some info related with this problem: http://www.i18nqa.com/debug/utf8-debug.html

Do you know any function to decode correctly?


回答1:


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


来源:https://stackoverflow.com/questions/21751893/asp-i-can%c2%b4t-decode-some-character-from-utf-8-to-iso-8859-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!