经典 ASP - 如何将 UTF-8 字符串转换为 UCS-2? [英] Classic ASP - How to convert a UTF-8 string to UCS-2?

查看:33
本文介绍了经典 ASP - 如何将 UTF-8 字符串转换为 UCS-2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SQL Server 中将 UTF-8 字符串存储为 UCS-2 时遇到问题.当我将其拉出以显示在内容类型设置为 UTF-8 的页面上时,它工作正常.但是我有一个第三方 Javascript 组件,当我将它传递给数据库的字符串时,它会将其呈现为 USC2.或者不是 UTF8.

I have a problem where I am storing a UTF-8 string in SQL Server as UCS-2. When I pull it out to display on a page with content-type set to UTF-8 it works fine. But I have a third party Javascript component which when I pass it the string for the database it renders it as USC2. Or not UTF8.

ASP中有没有办法把这个字符串从数据库中读取后转成UTF-8传递给第三方组件(混淆)?

Is there a way in ASP to convert this string to UTF-8 after reading it from the database to pass it to the third party component (obfuscated)?

希望这是有道理的.

推荐答案

我怀疑您是否违反了经典表单后字符编码不匹配问题.

My suspicion is you are falling foul of the classic form post character encoding mismatch problem.

事情是这样的:-

  • 您有一个使用 UTF-8 编码呈现给客户端的表单.
  • 因此,浏览器会发布使用 UTF-8 编码输入到表单中的文本值.
  • 接收帖子的操作页面将其 Response.Codepage 设置为典型的 OEM 代码页,例如 1252.
  • 发布的 UTF-8 字符串的每个字节都被服务器视为单个字符,而不是将 UTF-8 编码字节集解码为正确的 unicode 字符.
  • 该字符串与现在损坏的字符一起存储在数据库中.
  • 页面希望向客户端显示包含损坏字符的 DB 字段的内容.
  • 页面将其 CharSet 设置为 UTF-8,但其 Response.CodePage 保留在 OEM 代码页中,例如 1252.
  • Response.Write 用于将字段内容发送到客户端,Unicode 字符将转换回在早期帖子中接收到的字节集.
  • 客户端认为它得到了 UTF-8,因此它将从服务器接收到的字符解码为 UTF-8,就像它们原来一样,因此它们正确地出现在屏幕上.
  • 一切都很好,好像一切都很好,而这些字符只是通过 ASP 来回弹跳.一个页面中的错误在另一个页面中具有匹配的错误(可能是同一页面),这使得一切看起来都很好.

如果您直接使用 SQL 服务器工具检查字段内容,您可能会在那里看到损坏的字符串.现在您想将此字符串与另一个需要直接 unicode 字符串的组件一起使用,这就是您发现此错误的地方.

If you examine the field contents directly with SQL server tools you will likely see the corrupted strings there. Now that you want to use this string with another component which is expecting a straight-forward unicode string this is where you discover this bug.

解决方案是始终确保您的所有页面不仅在响应中发送 CharSet = "UTF-8",而且还在使用 Response.Write 和尝试读取任何 Request.Form 值之前使用 Response.CodePage = 65001.在 <%@ 页眉中使用 Codepage 指令.

The solution is to always ensure all your pages not only send CharSet = "UTF-8" in the response but also use Response.CodePage = 65001 before using Response.Write and before attempting to read any Request.Form values. Use Codepage directive in the <%@ page header.

现在您需要修复数据库中已有的损坏字符串.

Now you are left with repairing the corrupt strings already in your DB.

使用 ADODB.Stream:-

Use an ADODB.Stream:-

Function ConvertFromUTF8(sIn)

    Dim oIn: Set oIn = CreateObject("ADODB.Stream")

    oIn.Open
    oIn.CharSet = "WIndows-1252"
    oIn.WriteText sIn
    oIn.Position = 0
    oIn.CharSet = "UTF-8"
    ConvertFromUTF8 = oIn.ReadText
    oIn.Close

End Function

这个函数(顺便说一句,它是您实际问题的答案)接受一个损坏的字符串(一个具有字节表示的字节)并转换为它应该是的字符串.您需要将此转换应用于数据库中已成为该错误受害者的每个字段.

This function (which BTW is the answer to your actual question) takes a corrupted string (one that has the byte of byte representation) and converts to the string it should have been. You need to apply this transform to every field in the DB that has fallen victim to the bug.

这篇关于经典 ASP - 如何将 UTF-8 字符串转换为 UCS-2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆