如何在EXCEL 2010中将HEX转换为BIN,一次一个字符

我试图find一种方法来获取一串HEX值并将其转换为BIN。 我需要一次转换1个hex字符:

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

我需要在Excel中执行此操作。 任何帮助将是伟大的。

谢谢,拉里

在一个模块中

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 

是的,我最近不得不这样做。 我迟到了,但其他人不得不这样做,所以我会留下代码,让大家可以find它:

 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 

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

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

  • 数量必需。 您要转换的hex数字。 数字不能包含超过10个字符。 数字的最高有效位是符号位(右起第40位)。 其余的9位是幅度位。 负数用2的补码表示。
  • 地方可选。 要使用的字符数。 如果省略了地方,HEX2BIN使用必要的最less字符数。 用前导0(零)填充返回值是有用的。