【问题标题】:How can you convert HEX to BIN, one character at a time in EXCEL 2010如何将 HEX 转换为 BIN,在 EXCEL 2010 中一次一个字符
【发布时间】:2012-03-16 16:29:00
【问题描述】:

我正在尝试找到一种方法来获取一串 HEX 值并将它们转换为 BIN。我需要一次转换 1 个 HEX 字符:

例如:HEX = 0CEC BIN = 0000 1100 1110 1100

我需要在 Excel 中执行此操作。任何帮助都会很棒。

谢谢, 拉里

【问题讨论】:

    标签: excel vba hex bin


    【解决方案1】:

    在一个模块中:

    Public Function HEX2BIN(strHex As String) As String
        Dim c As Long, i As Long, b As String * 4, j As Long
        For c = 1 To Len(strHex)
            b = "0000"
            j = 0
            i = Val("&H" & Mid$(strHex, c, 1))
            While i > 0
                Mid$(b, 4 - j, 1) = i Mod 2
                i = i \ 2
                j = j + 1
            Wend
            HEX2BIN = HEX2BIN & b & " "
        Next
        HEX2BIN = RTrim$(HEX2BIN)
    End Function
    

    为:

    =HEX2BIN("0CEC")
       0000 1100 1110 1100
    

    【讨论】:

    • 谢谢.. 另外,我错了,HEX2BIN 显然可以工作。感谢您的帮助
    【解决方案2】:

    是的,我最近不得不这样做。我玩游戏迟到了,但其他人有时会这样做,所以我将代码留在每个人都可以找到的地方:

    Option Explicit
    
    Public Function HexToBinary(strHex As String, Optional PadLeftZeroes As Long = 5, Optional Prefix As String = "oX") As String
    Application.Volatile False
    
    ' Convert a hexadecimal string into a binary
    ' As this is for Excel, the binary is returned as string: there's a risk that it will be treated as a number and reformatted
    
    ' Code by Nigel Heffernan, June 2013. Http://Excellerando.Blogspot.co.uk  THIS CODE IS IN THE PUBLIC DOMAIN
    
    ' Sample Usage:
    '
    '   =HexToBinary("8E")
    '   oX0010001110
    '
    '   =HexToBinary("7")
    '   oX001111
    '
    '   =HexToBinary("&HD")
    '   oX01101
    
    
    Dim lngHex As Long
    Dim lngExp As Long
    Dim lngPad As Long
    Dim strOut As String
    Dim strRev As String
    
    
    If Left(strHex, 2) = "&H" Then
        lngHex = CLng(strHex)
    Else
        lngHex = CLng("&H" & strHex)
    End If
    
    lngExp = 1
    Do Until lngExp > lngHex
        ' loop, bitwise comparisons with successive powers of 2
        ' Where bitwise comparison is true, append "1", otherwise append 0
        strRev = strRev & CStr(CBool(lngHex And lngExp) * -1)
    lngExp = lngExp * 2
    Loop
    
    
    ' As we've done this in ascending powers of 2, the results are in reverse order:
    If strRev = "" Then
        HexToBinary = "0"
    Else
        HexToBinary = VBA.Strings.StrReverse(strRev)
    End If
    
    ' The result is padded by leading zeroes: this is the expected formatting when displaying binary data
    If PadLeftZeroes > 0 Then
        lngPad = PadLeftZeroes * ((Len(HexToBinary) \ PadLeftZeroes) + 1)
        HexToBinary = Right(String(lngPad, "0") & HexToBinary, lngPad)
    End If
    
    HexToBinary = Prefix & HexToBinary
    
    End Function
    

    【讨论】:

      【解决方案3】:

      您可以使用HEX2BIN(number, [places])

      HEX2BIN 函数语法具有以下参数:

      • 数字必填。要转换的十六进制数。数字不能超过 10 个字符。数字的最高有效位是符号位(右起第 40 位)。其余 9 位是幅度位。负数使用补码表示法表示。
      • 地点 可选。要使用的字符数。如果places 被省略,HEX2BIN 使用最少的必要字符数。 Places 对于用前导 0(零)填充返回值很有用。

      【讨论】:

      • 顺便说一句:“安装和加载分析工具包插件”
      • HEX2BIN 不适用于此。 HEX 值太长,EXCEL 无法运行 HEX2BIN
      • @KevinDTimm 抱歉,这是针对旧版本的 Excel :)
      【解决方案4】:

      我会使用一个简单的公式如下:

      =HEX2BIN(MID(S23,1,2))&HEX2BIN(MID(S23,3,2))&HEX2BIN(MID(S23,5,2))&HEX2BIN(MID(S23,7,2)&HEX2BIN(MID (S23,9,2)&HEX2BIN(MID(S23,11,2)&HEX2BIN(MID(S23,13,2))

      单元格 S23 = BFBEB991,结果 = 10111111101111101011100110010001

      这将允许它尽可能长的时间。只需添加所需的重复次数,将起始位置增加 2(例如 1、3、5、7、9、11、13、15 ......)。请注意,缺少的字符将被忽略。

      【讨论】:

        【解决方案5】:

        对我来说,它提供了这个(对不起,在 VBA 中,但优点是不询问你要转换的字符串的长度)。请注意,我在下部添加了注释,您可以在每个 4 位部分之间添加一个空格。有些人不想要这个空间,有些人会需要它:

        Length = Len(string_to_analyse)
        For i = 1 To Length
            Value_test_hexa = Left(Right(string_to_analyse, Length - (i - 1)), 1)
        'get the asci value of each hexa character (actually can work as a decimal to binary as well)
            Value_test = Asc(Value_test_hexa)
        
            If Value_test > 47 And Value_test < 58 Then
                Value_test = Value_test - 48
            End If
        
        ' Convert A to F letters to numbers from 10 to 15
            If Value_test > 64 And Value_test < 71 Then
                Value_test = Value_test - 55
            End If
        
        'identify the values of the 4 bits for each character (need to round down)
            a = WorksheetFunction.RoundDown(Value_test / 8, 0)
            b = WorksheetFunction.RoundDown((Value_test - a * 8) / 4, 0)
            c = WorksheetFunction.RoundDown((Value_test - a * 8 - b * 4) / 2, 0)
            d = (Value_test - a * 8 - b * 4 - c * 2)
        
            Value_converted = Value_converted & a & b & c & d ' can eventually add & " " in order to put a space every 4 bits
        Next i
        

        测试正常,您可以使用它。

        【讨论】:

          【解决方案6】:

          把它留在这里给任何需要它的人。 我没有手动将十六进制转换为二进制,而是使用 Excel 的内置 HEX2BIN 函数。

          Function hexToBin(hexStr As String) As String
              Dim i As Integer, b As String, binStr As String
              For i = 1 To Len(hexStr)
                  b = Application.hex2bin(Mid(hexStr, i, 1), 4)
                  binStr = binStr & b
              Next i
              hexToBin = binStr
          End Function
          

          【讨论】:

            猜你喜欢
            • 2022-06-14
            • 2021-09-05
            • 2019-05-10
            • 2020-06-02
            • 2015-01-13
            • 2018-01-31
            • 1970-01-01
            • 2018-03-20
            • 2016-08-28
            相关资源
            最近更新 更多