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

查看:351
本文介绍了经典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.

它是这样的: -


  • 您有一个形式,是psented使用UTF-8编码的客户端$ P $。

  • 结果浏览器的帖子文本值使用UTF-8编码输入到表单。

  • 接受职位的操作页面都有它的响应。codePAGE设置为1252典型的OEM codePAGE等。

  • 张贴的UTF-8字符串的每个字节是由服务器视为一个单独的字符,而不是套解码UTF-8的EN codeD字节到正确的UNI code字符。

  • 的字符串存储在DB与现在已损坏的字符。

  • 系统页面希望present到客户端包含损坏的字符的数据库字段的内容。

  • 页面设置它的字符集为UTF-8,但它的响应。codePAGE保持在1252的OEM codePAGE等。

  • 的Response.Write用于发送的字段内容到客户端,单向code字符转换回为一个字节设置为在更早期后接收的字节。

  • 的客户认为其获得UTF-8,因此它去codeS从服务器接收的字符为UTF-8,就像他们最初因此它们在屏幕上显示正确。

  • 一切都进行罚款,如果同时这些字符仅仅是被反弹来回通过ASP一切正常。在一个页面中的bug已经在其他的匹配错误(可能是在同一个页面),这使得一切看起来正常。

如果你直接使用SQL Server工具检查字段的内容,你可能会看到损坏的弦那里。现在,您要使用此字符串与期待一个直接的UNI code字符串的另一个组件这是你发现这个错误。

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.

解决方案是始终确保您所有的网页不仅发送字符集=UTF-8的反应,但也可以使用响应。codePAGE = 65001使用的Response.Write之前,并试图读取任何之前的Request.Form值。使用在&lt 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

这个函数(BTW是回答你的问题实际)需要一个损坏的字符串(一个具有字节重新presentation的字节),并将其转换为字符串它应该是。您需要将此转化为各个领域中已牺牲品的bug数据库。

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天全站免登陆