在Excel中使用DEC2BIN()创建列表

时间:2018-06-20 12:38:09

标签: excel binary type-conversion decimal

编辑:实际上,我实际上向后编写了单元格公式,现在并没有花费将其反转所需的时间。根据我的情况前进或后退都可以。

我需要创建一个“选项”列表,该列表使用32位十进制数字选择二进制选项。

示例:给出511,对应于0000 0000 0000 0000 0000 0000 0001 1111 1111

我想将此显示为“ 1 2 3 4 5 6 7 8 9”。

我有一个庞大的10页单元格公式可以执行此操作*(请参见下文摘录),但似乎必须有一种简单的方法。

有什么建议吗?

以下是我拥有的excel公式中的前两个术语:

  

“ == IF(LEFT(DEC2BIN(MOD(QUOTIENT(I6,256 ^ 3),256),8)&DEC2BIN(MOD(QUOTIENT(I6,256 ^ 2),256),8)&DEC2BIN(MOD(QUOTIENT (I6,256 ^ 1),256),8)&DEC2BIN(MOD(QUOTIENT(I6,256 ^ 0),256),8),1)   =“ 1”,“ 32”,“”)&IF(MID(DEC2BIN(MOD(QUOTIENT(I6,256 ^ 3),256),8)&DEC2BIN(MOD(QUOTIENT(I6,256 ^ 2),256), 8)&DEC2BIN(MOD(QUOTIENT(I6,256 ^ 1),256),8)&DEC2BIN(MOD(QUOTIENT(I6,256 ^ 0),256),8),2,1)   =“ 1”,“ 31”,“”)&...“

3 个答案:

答案 0 :(得分:0)

据我了解,您正在尝试将511改成0000 0000 0000 0000 0000 0001 1111 1111,每4次放置一个空格。很有可能不是这样,但是我在编写它时有一些相对的乐趣,所以这就是我得到的:

Option Explicit

Public Sub TestMe()

   Debug.Print DEC2BIN2(511)
   Debug.Print DEC2BIN2(511) = "0000 0000 0000 0000 0000 0001 1111 1111"   'True
   Debug.Print DEC2BIN2(502) = "0000 0000 0000 0000 0000 0001 1111 0110"   'True

End Sub

Public Function DEC2BIN2(inputVal As Long) As String

    Dim result As String
    Dim cnt As Long

    result = WorksheetFunction.Dec2Bin(inputVal)
    result = AddZeroes(8 * 4 - Len(result)) & result
    result = StrReverse(result)

    For cnt = 1 To 8
        result = Insert(result, " ", 4 * cnt + cnt)
    Next cnt

    DEC2BIN2 = Trim(StrReverse(result))

End Function

AddZeroes()返回带有N个零的字符串。 Insert()用于在特定位置插入空格:

Public Function AddZeroes(zeroes As Long) As String

    Dim cnt As Long        
    For cnt = 1 To zeroes
        AddZeroes = AddZeroes & "0"
    Next cnt

End Function

Function Insert(original As String, added As String, pos As Long) As String

    Insert = Mid(original, 1, pos - 1) _
                    & added _
                    & Mid(original, pos, Len(original) - pos + 1)

End Function

答案 1 :(得分:0)

请考虑以下功能:

Public Function Encoder(r As Range) As String
    Dim s As String
    Encoder = ""
    s = BigBinary(r)
    For i = 1 To 36
        If Mid(s, 37 - i, 1) = "1" Then Encoder = i & " " & Encoder
    Next i
End Function


Public Function BigBinary(r As Range) As String
    Dim addy As String, s1 As String, s2 As String

    addy = r.Address(0, 0)
    s1 = "=DEC2BIN(INT(A1/2^27),9)&DEC2BIN(INT(MOD(A1,2^27)/2^18),9)&DEC2BIN(INT(MOD(A1,2^18)/2^9),9)&DEC2BIN(MOD(A1,2^9),9)"
    s1 = Replace(s1, "A1", addy)
    s = Evaluate(s1)
    BigBinary = s
End Function

enter image description here

用户定义函数(UDF)易于安装和使用:

  1. ALT-F11弹出VBE窗口
  2. ALT-I ALT-M打开一个新模块
  3. 将内容粘贴并关闭VBE窗口

如果保存工作簿,则UDF将随之保存。 如果您在2003年以后使用Excel版本,则必须保存 该文件为.xlsm而不是.xlsx

要删除UDF:

  1. 如上调出VBE窗口
  2. 清除代码
  3. 关闭VBE窗口

要从Excel使用UDF,请执行以下操作:

= myfunction(A1)

要全面了解有关宏的更多信息,请参见:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

,有关UDF的详细信息,请参见:

http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx

Macros must be enabled for this to work!

答案 2 :(得分:0)

您可以屏蔽每个比特,例如

Function bitOrdRev(slong As String) As String
    Dim i As Long, x As Variant
    x = CDec(slong)
    For i = 0 To 32
        If Application.WorksheetFunction.Bitand(x, (2 ^ i)) <> 0& Then bitOrdRev = bitOrdRev & (i + 1) & " "
    Next
    bitOrdRev = RTrim$(bitOrdRev)
End Function

这假定为无符号值,因此允许设置所有32位而没有符号位(bitOrdRev("4294967295")1..32