無限不可能性ドライブ

『ニューラルネットワーク自作入門』に刺激されてExcelVBAでニューラルネットワークを作ってみたものの、やっぱり数学やらなきゃと思い少しずつやってきたのもあって、自分の知識の整理とかそういった感じです。

【VBA】2進数を文字列に変換する(前回の続き)

f:id:celaeno42:20210312230952p:plain

Option Explicit

Public Sub 読み込んだ2進数を文字列に変換()
    Dim filePath As String
    Dim fileNo As Long
    Dim txt As String
    Dim txts() As String
    Dim i As Long

    filePath = ThisWorkbook.Path & "\binary.dat"
    
    fileNo = FreeFile
    Open filePath For Input As #fileNo
        Line Input #fileNo, txt
    Close #fileNo

    txt = decode(txt)
    txts = Split(txt, vbCrLf)
    
    For i = 0 To UBound(txts)
        Debug.Print txts(i)
    Next
    
End Sub

'2進数から文字列に変換
Private Function decode(ByVal txt As String) As String
    Dim i As Long
    Dim bin As String
    Dim res As String
    
    For i = 1 To Len(txt) Step 8
        bin = Mid(txt, i, 8)
        res = res & Chr(WorksheetFunction.Bin2Dec(bin))
    Next
    
    decode = res
    
End Function