必要に迫られてExcel VBAで2進数表示をしたかったのですが、VBAのDEC2BINでは桁数が10桁までしか対応しておらず、そのうえなぜか値は-512~511と桁数とはなんら関連のない範囲だけ。諦めて自作しました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
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 |