VBS字符串编码转换函数代码(vb字符串转换为文本类型)墙裂推荐

随心笔谈2年前发布 编辑
166 0
🌐 经济型:买域名、轻量云服务器、用途:游戏 网站等 《腾讯云》特点:特价机便宜 适合初学者用 点我优惠购买
🚀 拓展型:买域名、轻量云服务器、用途:游戏 网站等 《阿里云》特点:中档服务器便宜 域名备案事多 点我优惠购买
🛡️ 稳定型:买域名、轻量云服务器、用途:游戏 网站等 《西部数码》 特点:比上两家略贵但是稳定性超好事也少 点我优惠购买

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=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=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

© 版权声明

相关文章