How can you convert HEX to BIN, one character at a time in EXCEL 2010

安稳与你 提交于 2019-11-27 07:24:54

问题


I am trying to find a way to take a string of HEX values and convert them to BIN. I need to convert 1 HEX character at a time:

For example: HEX = 0CEC BIN = 0000 1100 1110 1100

I need to do this in Excel. Any help would be great.

Thanks, Larry


回答1:


In a module

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

For

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



回答2:


Yes, I had to do this recently. I'm late to the game, but other people will have to do this from time to time, so I'll leave the code where everyone can find it:

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:


You can use HEX2BIN(number, [places]).

The HEX2BIN function syntax has the following arguments:

  • Number Required. The hexadecimal number you want to convert. Number cannot contain more than 10 characters. The most significant bit of number is the sign bit (40th bit from the right). The remaining 9 bits are magnitude bits. Negative numbers are represented using two's-complement notation.
  • Places Optional. The number of characters to use. If places is omitted, HEX2BIN uses the minimum number of characters necessary. Places is useful for padding the return value with leading 0s (zeros).



回答4:


I would use a simple formula as follows:

=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))

cell S23 = BFBEB991, Result = 10111111101111101011100110010001

This would allow it to be as long you need. Just add as many repetitions as you need incrementing the start position by 2 (eg 1, 3, 5, 7, 9, 11, 13, 15, ....). Note that the missing characters will be ignored.




回答5:


For me, it gives this (sorry, in VBA, but has the advantage of not asking you the length of your string to convert). Be careful, I put a comment in the lower part for which you can add a space between each section of 4 bits. Some don't want the space and some will need it:

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

Tested OK so you can go with it.



来源:https://stackoverflow.com/questions/9741003/how-can-you-convert-hex-to-bin-one-character-at-a-time-in-excel-2010

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!