How to Convert from decimal to binary
Posted on August 20, 2011
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 'Coding Basics - How to Convert from decimal to binary Function Bin(ByVal value As Long) As String Dim result As String, exponent As Integer ' This is faster than creating the string by appending chars. result = String$(32, "0") Do If value And Power2(exponent) Then ' We found a bit that is set, clear it. Mid$(result, 32 - exponent, 1) = "1" value = value Xor Power2(exponent) End If exponent = exponent + 1 Loop While value Bin = Mid$(result, 33 - exponent) ' Drop leading zeros. End Function |