EXCEL VBAメモ - 文字をUTF-8バイト列に変換する
ADODB.Streamオブジェクトにテキストで書き込んで、Typeプロパティをバイナリに変えてByteの配列として読み出す。
なお変換されたUTF-8バイト列にはBOMが付加されている。
Sub Sample() Dim buf() As Byte Dim s As String Dim i As Long With CreateObject("ADODB.Stream") .Mode = 3 'adModeReadWrite .Open .Type = 2 ' adTypeText .Charset = "UTF-8" .WriteText "あ" .Position = 0 ' .Typeを変更するため0にする必要がある .Type = 1 'adTypeBinary buf = .Read .Close End With s = "" For i = 0 To UBound(buf) s = s & " " & Hex(buf(i)) Next MsgBox s ' EF BB BF E3 81 82 'BOMが付加されている End Sub