Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
939 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...