10進数を2進数で・・・Excelの話。
必要に迫られてExcel VBAで2進数表示をしたかったのですが、VBAのDEC2BINでは桁数が10桁までしか対応しておらず、そのうえなぜか値は-512~511と桁数とはなんら関連のない範囲だけ。諦めて自作しました。
Function Dec2BinEx(x As Double, column As Integer) As String
Dim ret As String
Dim hosu As Boolean
Dim y As Integer
Dim kuriage As Integer
If column < 1 Then
Dec2BinEx = ""
Exit Function
End If
ret = ""
If x < 0 Then
x = (-x)
hosu = True
kuriage = 1
Else
hosu = False
kuriage = 0
End If
Do
If hosu = False Then
ret = Format(x - Int(x / 2) * 2) & ret
Else
y = (1 - (x - Int(x / 2) * 2)) + kuriage
kuriage = y / 2
ret = Format(y Mod 2) & ret
End If
x = Int(x / 2)
column = column - 1
Loop While x >= 0 And column > 0
While column > 0
If hosuu = False Then
ret = "0" & ret
Else
ret = "1" & ret
End If
column = column - 1
Wend
Dec2BinEx = ret
End Function