这篇“VBS字符串编码转换函数代码怎么写”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“VBS字符串编码转换函数代码怎么写”文
这篇“VBS字符串编码转换函数代码怎么写”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“VBS字符串编码转换函数代码怎么写”文章吧。
因为业务需要将一些字符串转换为指定编码方便后期操作
核心代码
Const adTypeBinary = 1Const adTypeText = 2' accept a string and convert it to Bytes array in the selected CharsetFunction 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 = NothingEnd Function' accept Bytes array and convert it to a string using the selected charsetFunction 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 = NothingEnd 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
使用例子:
dim s1,s2,FromCharset,ToCharsets1 = "我的字符串之编程网"FromCharset = "GB2312"ToCharset = "ISO-8859-1"s2 = AlterCharset(s1,FromCharset,ToCharset)
以上就是关于“VBS字符串编码转换函数代码怎么写”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注编程网精选频道。
--结束END--
本文标题: VBS字符串编码转换函数代码怎么写
本文链接: https://www.lsjlt.com/news/357116.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0