nKB

nKB

シンプルにいこうよ

VBA 10進数を2進数に変換する関数

Function ConvToBin(Number As Long) As String

Dim binCnt As Long
Dim DeciNum As Long 'Decimal Number
Dim i As Long

    DeciNum = Number
    
    '累乗確認
    binCnt = 0
    While 2 ^ binCnt <= DeciNum ' <=(以下の場合)
        binCnt = binCnt + 1
    Wend
    
    '2進数作成
    For i = binCnt - 1 To 0 Step -1
        If DeciNum >= 2 ^ i Then
            ConvToBin = ConvToBin & "1"
            DeciNum = DeciNum - 2 ^ i
        Else
            ConvToBin = ConvToBin & "0"
        End If
    Next

End Function
広告を非表示にする