VBS字符串編碼轉(zhuǎn)換函數(shù)代碼
因?yàn)闃I(yè)務(wù)需要將一些字符串轉(zhuǎn)換為指定編碼方便后期操作
核心代碼
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使用例子:
dim s1,s2,FromCharset,ToCharset s1 = "我的字符串之腳本之家" FromCharset = "GB2312" ToCharset = "ISO-8859-1" s2 = AlterCharset(s1,FromCharset,ToCharset)
到此這篇關(guān)于VBS字符串編碼轉(zhuǎn)換函數(shù)代碼的文章就介紹到這了,更多相關(guān)VBS編碼轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vbs小程序圖標(biāo)更改方法的實(shí)現(xiàn)
這篇文章主要介紹了vbs小程序圖標(biāo)更改方法的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
VBS教程:函數(shù)-FormatCurrency 函數(shù)
VBS教程:函數(shù)-FormatCurrency 函數(shù)...2006-11-11
VC中實(shí)現(xiàn)文字豎排的簡(jiǎn)單方法(推薦)
下面小編就為大家?guī)硪黄猇C中實(shí)現(xiàn)文字豎排的簡(jiǎn)單方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05
視頻轉(zhuǎn)換大師WinMPG Video Convert 6.63
視頻轉(zhuǎn)換大師WinMPG Video Convert 6.63...2007-04-04

