gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:41:51 29 4
gpt4 key购买 nike

我的经典 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com