VBScriptでBase64やHex、バイナリを扱う関数をまとめてみた

投稿者: | 2016-01-28

VBScriptでBase64やHex、バイナリを扱う関数をまとめてみました。
今後テーマにする予定のパスワード保管や暗号化を扱う際にも使えるかなと思ったもので。

ExcelなどのVBAにも転用できるで、割りと便利かと。
でも、VBScriptでバイナリを扱おうとする人って普通はいないだろうなあ。
f:id:Fio:20160128005655p:plain

文字列をバイナリに変換

Function String2Bytes(strText)
Set objEncoder = CreateObject("System.Text.UTF8Encoding")
String2Bytes = objEncoder.GetBytes_4(strText)
Set objEncoder = Nothing
End Function

バイナリを文字列に変換

Function Bytes2String(Bytes)
Set objEncoder = CreateObject("System.Text.UTF8Encoding")
Bytes2String = objEncoder.GetString(*1)
Set objEncoder = Nothing
End Function

文字列をバイナリに変換(Shift_JIS)

Function SJIS2Bytes(strText)
Set objStream = CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 2  'adTypeText
objStream.Charset = "Shift_JIS"
objStream.WriteText strText
objStream.Position = 0
objStream.Type = 1  'adTypeBinary
SJIS2Bytes = objStream.Read
objStream.Close
Set objStream = Nothing
End Function

バイナリを文字列に変換(Shift_JIS)

Function Bytes2SJIS(Bytes)
Set objStream = CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1  'adTypeBinary
objStream.Write Bytes
objStream.Position = 0
objStream.Type = 2  'adTypeText
objStream.Charset = "Shift_JIS"
Bytes2SJIS = objStream.ReadText
objStream.Close
Set objStream = Nothing
End Function

バイナリをBase64に変換

Function Bytes2Base64(Bytes)
Set objElement = CreateObject("Msxml2.DOMDocument").createElement("base64")
objElement.dataType = "bin.base64"
objElement.nodeTypedValue = Bytes
Bytes2Base64 = objElement.text
Set objElement = Nothing
End Function

Base64をバイナリに変換

Function Base642Bytes(strText)
Set objElement = CreateObject("Msxml2.DOMDocument").createElement("base64")
objElement.dataType = "bin.base64"
objElement.text = strText
Base642Bytes = objElement.nodeTypedValue
Set objElement = Nothing
End Function

バイナリをHexに変換

Function Bytes2Hex(Bytes)
set objElement = CreateObject("Msxml2.DOMDocument").createElement("hex")
objElement.dataType = "bin.hex"
objElement.nodeTypedValue = Bytes
Bytes2Hex = objElement.text
Set objElement = Nothing
End Function

Hexをバイナリに変換

Function Hex2Bytes(strText)
set objElement = CreateObject("Msxml2.DOMDocument").createElement("hex")
objElement.dataType = "bin.hex"
objElement.text = strText
Hex2Bytes = objElement.nodeTypedValue
Set objElement = Nothing
End Function

ファイルをバイナリに読み込む

Function ReadBinaryFile(FilePath)
Set objStream = CreateObject("ADODB.Stream")
objStream.Type = 1 'adTypeBinary
objStream.Open
objStream.LoadFromFile FilePath
ReadBinaryFile = objStream.Read
objStream.Close
Set objStream = Nothing
End Function

バイナリをファイルに書き込む

Sub SaveBinaryData(Bytes, FilePath)
Set objStream = CreateObject("ADODB.Stream")
objStream.Type = 1 'adTypeBinary
objStream.Open
objStream.Write Bytes
objStream.SaveToFile FilePath, 2 'adSaveCreateOverWrite
objStream.Close
Set objStream = Nothing
End Sub

テスト用

sText = "雨が降ってゐる"
bData = String2Bytes(sText)
sBase64= Bytes2Base64(bData)
MsgBox sBase64
bData = Base642Bytes(sBase64)
sText = Bytes2String(bData)
MsgBox sText
bData = String2Bytes(sText)
sBase64= Bytes2Hex(bData)
MsgBox sBase64
bData = Hex2Bytes(sBase64)
sText = Bytes2String(bData)
MsgBox sText

テスト用2

sText = "雨が降ってゐる"
bData = SJIS2Bytes(sText)
sBase64= Bytes2Base64(bData)
MsgBox sBase64
bData = Base642Bytes(sBase64)
sText = Bytes2SJIS(bData)
MsgBox sText
bData = SJIS2Bytes(sText)
sBase64= Bytes2Hex(bData)
MsgBox sBase64
bData = Hex2Bytes(sBase64)
sText = Bytes2SJIS(bData)
MsgBox sText

*1: Bytes