string - 将 UTF-8 字符串转换为 ISO-8859-1

标签 string vbscript asp-classic character-encoding

我的经典 ASP 应用程序从数据库中检索 UTF-8 字符串,但我需要将其转换为 ISO-8859-1。我无法更改 HTML 页面编码;

我真的需要只转换获取的字符串。我该怎么做?

最佳答案

我找到了答案here :

Const adTypeBinary = 1
Const adTypeText = 2

' accept a string and convert it to Bytes array in the selected Charset
Function StringToBytes(Str,Charset)
  Dim Stream : Set Stream = Server.CreateObject("ADODB.Stream")
  Stream.Type = adTypeText
  Stream.Charset = Charset
  Stream.Open
  Stream.WriteText Str
  Stream.Flush
  Stream.Position = 0
  ' rewind stream and read Bytes
  Stream.Type = adTypeBinary
  StringToBytes= Stream.Read
  Stream.Close
  Set Stream = Nothing
End Function

' accept Bytes array and convert it to a string using the selected charset
Function BytesToString(Bytes, Charset)
  Dim Stream : Set Stream = Server.CreateObject("ADODB.Stream")
  Stream.Charset = Charset
  Stream.Type = adTypeBinary
  Stream.Open
  Stream.Write Bytes
  Stream.Flush
  Stream.Position = 0
  ' rewind stream and read text
  Stream.Type = adTypeText
  BytesToString= Stream.ReadText
  Stream.Close
  Set Stream = Nothing
End Function

' This will alter charset of a string from 1-byte charset(as windows-1252)
' to another 1-byte charset(as windows-1251)
Function AlterCharset(Str, FromCharset, ToCharset)
  Dim Bytes
  Bytes = StringToBytes(Str, FromCharset)
  AlterCharset = BytesToString(Bytes, ToCharset)
End Function

所以我就这样做了:

AlterCharset(str, "ISO-8859-1", "UTF-8")

效果很好。

关于string - 将 UTF-8 字符串转换为 ISO-8859-1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28834528/

相关文章:

vbscript - 如何使用 VBScript 关闭特定文件夹?

arrays - 一个类包含一个对象数组。我是否需要在终止包含类时将它们中的每一个都设置为空?

vbscript - 计算字符串中有多少个特定字符

java - 如果字符串包含大于 Integer.MAX_VALUE 的数字

c++ - 如何跳过输入流 C++ 中的前导空格?

java - 在java中解密在android应用程序中加密的字符串

xml - 使用 ASP/VB 获取节点属性的值

javascript - 传递空字符串时返回空数组

object - 克隆VBScript Err对象

iis - 如何以编程方式(在 vbscript 中)更改/设置 IIS 下站点的 tcp 端口?