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)文章
15分鐘學(xué)會(huì)vbscript中的正則表達(dá)式
這篇文章主要介紹了15分鐘學(xué)會(huì)vbscript中的正則表達(dá)式,需要的朋友可以參考下2018-06-06
vbscript Registry 注冊(cè)表操作實(shí)現(xiàn)代碼(讀寫(xiě)刪除)
vbscript Registry 注冊(cè)表操作實(shí)現(xiàn)代碼(讀寫(xiě)刪除)功能代碼,需要的朋友可以參考下。2011-12-12
用vbs實(shí)現(xiàn)在啟動(dòng) Windows 資源管理器時(shí)打開(kāi)特定文件夾
首先創(chuàng)建 Wscript.Shell 對(duì)象的實(shí)例;它是 Windows Script Host 對(duì)象,我們用它來(lái)在另一腳本內(nèi)運(yùn)行腳本或可執(zhí)行文件2007-03-03
用vbs 實(shí)現(xiàn)從剪貼板中抓取一個(gè) URL 然后在瀏覽器中打開(kāi)該 Web 站點(diǎn)
用vbs 實(shí)現(xiàn)從剪貼板中抓取一個(gè) URL 然后在瀏覽器中打開(kāi)該 Web 站點(diǎn)...2007-04-04
vbs(asp) ByVal ByRef函數(shù)調(diào)用使用說(shuō)明
ByVal 傳送參數(shù)內(nèi)存的一個(gè)拷貝給被調(diào)用者。也就是說(shuō),棧中壓入的直接就是所傳的值。 ByRef 傳送參數(shù)內(nèi)存的實(shí)際地址給被調(diào)用者。也就是說(shuō),棧中壓入的是實(shí)際內(nèi)容的地址。被調(diào)用者可以直接更改該地址中的內(nèi)容。2008-09-09

