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

问题描述 投票:1回答:5

我试图找到一种方法来获取一串HEX值并将它们转换为BIN。我需要一次转换1个十六进制字符:

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

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

谢谢,拉里

excel hex bin
5个回答
8
投票

在一个模块中

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

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

1
投票

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

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

  • 需要数量。要转换的十六进制数。数字不能超过10个字符。最重要的数字是符号位(右起第40位)。剩下的9位是幅度位。负数用二进制补码表示。
  • 地方可选。要使用的字符数。如果省略places,则HEX2BIN使用所需的最少字符数。使用位置对于使用前导0(零)填充返回值非常有用。

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


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

测试好,所以你可以使用它。

© www.soinside.com 2019 - 2024. All rights reserved.