大量使用DEC2BIN()

我试图在Excel 4503599627370495转换为二进制。 DEC2BIN()返回#NUM! 错误,因为DEC2BIN不能处理这么大的数字。

任何想法,我怎么能够使其工作?

Thanx JustinDavies – 这正是我所需要的,但是如果传递一个-ve数字,它会进入无限循环。 我的修改:

 Function DecToBin(ByVal DecimalIn As Variant, Optional NumberOfBits As Variant) As String DecToBin = "" DecimalIn = CDec(DecimalIn) If DecimalIn < 0 Then DecToBin = "Error - Number negative" Exit Function End If Do While DecimalIn <> 0 DecToBin = Trim$(Str$(DecimalIn - 2 * Int(DecimalIn / 2))) & DecToBin DecimalIn = Int(DecimalIn / 2) Loop If Not IsMissing(NumberOfBits) Then If Len(DecToBin) > NumberOfBits Then DecToBin = "Error - Number too large for bit size" Else DecToBin = Right$(String$(NumberOfBits, "0") & _ DecToBin, NumberOfBits) End If End If End Function 

见VBA张贴在这里

 ' The DecimalIn argument is limited to 79228162514264337593543950245 ' (approximately 96-bits) - large numerical values must be entered ' as a String value to prevent conversion to scientific notation. Then ' optional NumberOfBits allows you to zero-fill the front of smaller ' values in order to return values up to a desired bit level. Function DecToBin(ByVal DecimalIn As Variant, Optional NumberOfBits As Variant) As String DecToBin = "" DecimalIn = CDec(DecimalIn) Do While DecimalIn <> 0 DecToBin = Trim$(Str$(DecimalIn - 2 * Int(DecimalIn / 2))) & DecToBin DecimalIn = Int(DecimalIn / 2) Loop If Not IsMissing(NumberOfBits) Then If Len(DecToBin) > NumberOfBits Then DecToBin = "Error - Number too large for bit size" Else DecToBin = Right$(String$(NumberOfBits, "0") & _ DecToBin, NumberOfBits) End If End If End Function 
 =DEC2BIN(MOD(QUOTIENT($A$1,256^3),256),8)&DEC2BIN(MOD(QUOTIENT($A$1,256^2),256),8)&DEC2BIN(MOD(QUOTIENT($A$1,256^1),256),8)&DEC2BIN(MOD(QUOTIENT($A$1,256^0),256),8) 

由Taosique 在Excel中为大数字回答十进制的二进制转换 。

对于高达4,294,967,295的正数,您可以使用此答案中给出的公式。 如果你需要更大的数字,他的答案可以延长。