我正在尝试将4503599627370495
转换为Excel中的二进制文件。 DEC2BIN()返回#NUM!错误,因为DEC2BIN无法处理如此大的数字。
关于我如何能够使其发挥作用的任何想法?
答案 0 :(得分: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
答案 1 :(得分:4)
见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
答案 2 :(得分:2)
=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提供了回复重复Decimal to binary conversion for large numbers in Excel。
答案 3 :(得分:0)
我需要一个VBA函数将Excel十进制整数转换为二进制,因为MS再次失败了。我想出了以下内容,但我不得不接受一串&amp;零作为输出,这对我来说没问题。它使用log base 2,它可以是唯一的(或不是?),并按原样适用于所有正整数。
Function Dec_Bin(dx As Integer) As String
Dim x As Integer
Dim y As Long
Dim z As Integer
Dim zz As Double
Dim ch As String
Dim str As String, s1 As String
Dim lead As Boolean
ch = String(15, "0")
' Stop
zz = Log(dx) / Log(2)
z = Fix(zz)
y = dx - 2 ^ z
z = 15 - z
Mid(ch, z, 1) = "1"
While y > 0
zz = Log(y) / Log(2)
z = Fix(zz)
y = y - 2 ^ z
z = 15 - z
Mid(ch, z, 1) = "1"
Wend
ch = ch & "B"
Dec_Bin = ch
End Function
答案 4 :(得分:0)
此函数将转换为Double可以容纳的最大大小。 不过,我没有尝试使用负值。
Function cn(ByVal n As Double, ByVal s As Double)
'n the number to convert
's the numberic system to convert to.
'This function can convert to binary all the way to the length of the
'digits string and all in between.
Dim x As Double 'The exponent without decimals
Dim xx As Double 'The exponent with decimals, if any
Dim r As String 'The return string
Dim p As Integer 'Posistion of the digit in the return string
Dim L As Long 'Length of the string return string
Dim d '(d+1) because mid() does not accept 0.
'The position of the digit in the digits string.
Dim v As Double 'The numeric value of the position
'of the digit in the return string
Dim digits As String
digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Start:
If n > 0 Then
xx = Log(n) / Log(s)
x = Int(xx)
End If
p = x + 1
If r = "" Then
r = String(p, "0")
L = p
End If
v = s ^ x
d = n \ v
Mid(r, L - x, 1) = Mid(digits, d + 1, 1)
n = n - (v * d)
If n <> 0 Then GoTo Start
cn = r
End Function
答案 5 :(得分:0)
通常,当我需要知道十进制值的二进制数时,总是很有趣的看到十六进制值。该函数不使用MOD或HEX()作为大数,因为Microsoft对其进行了限制,因此对于非常大的数,它是手动完成的。
Bino(值,字节)
值可以是任何小数或超大整数整数或包含它的单元格。 字节是可选的,标准2,自动。
= Bino(A1,4)
显示的数字或字节数和比特是根据其值自动生成的,标准的最小值为2字节,但是您可以使用可选的 bytes 参数在左侧填充额外的零字节。上面的示例将显示A1具有4个字节和32位,即使A1包含一个数字,或者如果大于4,则将使用更多的字节和位。
= Bino(A1)
将显示最小为2个字节和16位或更大(如果需要)的A1。 为了更好地可视化,二进制格式将半字节用“-”分隔,将字节用“ |”分隔
= Bino(129)= 0x0081 = 0000-0000 | 1000-0001
= Bino(129,1)= 0x81 = 1000-0001
= Bino(257,1)= 0x0101 = 0000-0001 | 0000-0001
该函数可将错误消息插入CELL,以防数字为负数或字节>10。可以轻松删除或更改。
在测试VBA时,它为8位微控制器(AVR)装配优化代码模拟了大量数学运算,为16位精度的Sin(x)和Ln(x)创建例程,对我有帮助。
Public Function Bino(ByVal Valo As Double, Optional ByVal Bytes As Long = 2) As String
Dim Conta
Dim Resul as String
Dim Bits
Dim SA As Double
Dim SB As Double
Dim SX As String
If Valo < 0 Then
Bino = "[Err: Negative]"
Exit Function
End If
If Bytes > 4 Then
Bino = "[Err: Bytes > 10]"
Exit Function
End If
ValoHex = ""
SB = Valo
Do While SB > 1
SA = SB / 16
ValoHex = Hex(Int(16 * (SA - Int(SA)))) & ValoHex
SB = SA
Loop
If Len(ValoHex) Mod 2 = 1 Then ValoHex = "0" & ValoHex
Zeroz = Bytes * 2 - Len(ValoHex)
If Zeroz = 2 Then ValoHex = "00" & ValoHex
If Zeroz = 4 Then ValoHex = "0000" & ValoHex
ValoHexLen = Len(ValoHex)
ValoHex = "0x" & ValoHex
If Bytes < ValoHexLen Then Bytes = ValoHexLen / 2
Bits = Bytes * 8 - 1
For Conta = 0 To Bits
Div = ""
If Conta And Conta Mod 4 = 0 Then Div = "-"
If Conta And Conta Mod 8 = 0 Then Div = "|"
If Int(Valo / 2) = Valo / 2 Then Bitt = 0 Else Bitt = 1
Resul = Bitt & Div & Resul
Valo = Int(Valo / 2)
Next Conta
Resul = ValoHex & " = " & Resul
Bino = Resul
End Function
答案 6 :(得分:0)
这非常简单,Base(...)函数可以为您提供帮助。
BASE(CELL, 2)
第二个参数2用于二进制,您可以将其转换为其他相关的基数,例如十六进制,十月
答案 7 :(得分:0)
这非常简单,而且dec2bin应该如何工作
= BASE(CELL,2)
答案 8 :(得分:0)
我稍微修改了AndruWitta的修改以处理负数,并以二进制补码形式返回二进制数。
Function DecToBin(ByVal DecimalIn As Variant, Optional NumberOfBits As Variant) As String
DecToBin = ""
DecimalIn = CDec(DecimalIn)
If DecimalIn < 0 Then
DecimalIn = -DecimalIn - 1
Do While DecimalIn <> 0
DecToBin = Trim$(Str$(Not DecimalIn - 2 * (Int(DecimalIn / 2 + 1)))) & DecToBin
DecimalIn = Int(DecimalIn / 2)
Loop
DecToBin = Trim$(Str$(1)) & DecToBin
Else
Do While DecimalIn <> 0
DecToBin = Trim$(Str$(DecimalIn - 2 * Int(DecimalIn / 2))) & DecToBin
DecimalIn = Int(DecimalIn / 2)
Loop
End If
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
答案 9 :(得分:0)
这是一个Excel文件,可以在不使用VBA的情况下处理正负16位数字(sint16),请在此处查看我的答案: https://stackoverflow.com/a/64261306/2738240