1

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

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

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

谢谢,拉里

4

6 回答 6

9

在一个模块中:

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
于 2012-03-16T16:48:28.400 回答
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
于 2014-03-21T16:48:26.623 回答
1

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

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

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

对我来说,它给出了这个(对不起,在 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

测试OK,所以你可以用它。

于 2019-03-08T11:54:07.427 回答
0

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

=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 ......)。请注意,缺少的字符将被忽略。

于 2018-12-01T15:51:27.477 回答
0

把它留在这里给任何需要它的人。我没有手动将十六进制转换为二进制,而是使用 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
于 2021-06-24T07:16:59.120 回答