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
はい、私は最近これをしなければなりませんでした。私はゲームに遅れていますが、他の人が時々これを行う必要があるため、誰もが見つけられるようにコードを残します。
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 関数の構文には、次の引数があります。
- 番号必須。変換する 16 進数。数字は 10 文字を超えることはできません。数値の最上位ビットは符号ビット (右から 40 ビット目) です。残りの 9 ビットはマグニチュード ビットです。負の数は、2 の補数表記を使用して表されます。
- 場所オプション。使用する文字数。場所を省略した場合、HEX2BIN は必要最小限の文字数を使用します。Places は、先頭の 0 (ゼロ) で戻り値をパディングするのに役立ちます。
必要な人のためにここに置いておきます。手動で 16 進数から 2 進数に変換する代わりに、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
次のような簡単な式を使用します。
=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、...)。欠落している文字は無視されることに注意してください。
私にとっては、これが得られます(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をテストしました。