使用大数字的DEC2BIN()

use*_*131 12 binary excel function decimal

我正在尝试4503599627370495在Excel中转换为二进制文件.DEC2BIN()返回#NUM!错误,因为DEC2BIN无法处理如此大的数字.

有关如何使其工作的任何想法?

And*_*tta 6

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
Run Code Online (Sandbox Code Playgroud)


Alb*_*int 6

这非常简单, Base(...) 函数可以帮助您。

BASE(CELL, 2)
Run Code Online (Sandbox Code Playgroud)

第二个参数 2 用于二进制,您可以将其转换为其他相关基数,如 Hex、Oct


Jus*_*ies 5

请参阅此处发布的 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
Run Code Online (Sandbox Code Playgroud)