我需要在PHP中读取二进制文件,搜索位置,反转它们的位并操纵结果。
目前我的代码如下:
$src_file = "firmware/shapeshifter_v2.02.jic";
$offset = 983195;
if ($src_handle = fopen($src_file,"rb")) {
fseek($src_handle, $offset);
$src_content = fread($src_handle, 1024);
fclose($src_handle);
$src_content = $src_content & 128 >> 7 + $src_content & 64 >> 5 + $src_content & 32 >> 3 + $src_content & 16 >> 1 + $src_content & 8 << 1 + $src_content & 4 << 3 + $src_content & 2 << 5 + $src_content & 1 << 7;
print bin2hex($src_content);
}
我试图用这个论坛中发现的algorythm来反转chunk中的所有位,但是我不能用它反转多一点。
你是否有一个有效的解决方案来反转所有在PHP中的二进制变量中的位?
这是手动转换的样子:
答案 0 :(得分:0)
您将1024个字节读入字符串,而不是将单个字节读入数字。使用:
$src_content = ord(fread($src_handle, 1));
这将读取一个字节,然后将其从字符转换为其数字代码。然后你可以反转这个数字中的位。
答案 1 :(得分:0)
要反转二进制值中的位,请使用:
Option Explicit
'Set reference to Windows Script Host Object Model
' Microsoft Scripting Runtime
Sub GetDirList(bInclFiles As Boolean)
Const sDIRargs As String = " /A-S-L-H /S"
Dim sBaseFolder As String, sTempFile As String
Dim WSH As WshShell
Dim sCMD As String
Dim lErrCode As Long
Dim FSO As FileSystemObject, TS As TextStream
Dim S As String, sFN As String
Dim V As Variant, W As Variant
Dim I As Long
Dim lMaxLevel As Long
Dim lMinLevel As Long
Dim dctTrees As Dictionary, cT As cTree
Dim wsRes As Worksheet
Dim vRes As Variant, rRes As Range
'Add worksheet if needed
On Error Resume Next
Set wsRes = Worksheets("TreeLink")
If Err.Number = 9 Then
Set wsRes = Worksheets.Add
wsRes.Name = "TreeLink"
End If
On Error GoTo 0
Set rRes = wsRes.Cells(1, 1)
'Many ways to set starting point
sBaseFolder = Environ("HOMEDRIVE") & "\"
sTempFile = Environ("TEMP") & "\DirList.txt"
'CommandLine
sCMD = "CMD /c dir """ & sBaseFolder & """" & sDIRargs & " > " & sTempFile
Set WSH = New WshShell
lErrCode = WSH.Run(sCMD, xlHidden, True)
If Not lErrCode = 0 Then
MsgBox "Error in execution: Code - " & lErrCode
Stop
Else
'Read in the relevant data
Set dctTrees = New Dictionary
Set FSO = New FileSystemObject
Set TS = FSO.OpenTextFile(sTempFile, ForReading, False, TristateUseDefault)
lMaxLevel = 0
V = Split(TS.ReadAll, vbCrLf)
For I = 0 To UBound(V)
Do Until V(I) Like " Directory of *"
If I = UBound(V) Then Exit For
I = I + 1
Loop
Set cT = New cTree
S = Mid(V(I), 15)
'Can exclude certain directories at this point
'To exclude all that start with a dot:
If Not S Like "*\.*" Then
With cT
.FullPath = S
.FolderName = Right(S, Len(S) - InStrRev(S, "\"))
.Level = Len(S) - Len(Replace(S, "\", ""))
lMaxLevel = IIf(lMaxLevel > .Level, lMaxLevel, .Level)
dctTrees.Add Key:=S, Item:=cT
I = I + 1
'Only run for file list
If bInclFiles = True Then
Do
sFN = V(I)
If Not sFN Like "*<DIR>*" _
And sFN <> "" Then
'add the files
dctTrees(S).ADDfile Mid(sFN, 40)
End If
I = I + 1
Loop Until V(I) Like "*# File(s)*"
End If
End With
End If 'End of directory exclusion "if" statement
Next I
lMinLevel = dctTrees(dctTrees.Keys(0)).Level
I = 0
With rRes.Resize(columnsize:=lMaxLevel + 1).EntireColumn
.Clear
.HorizontalAlignment = xlLeft
End With
Application.ScreenUpdating = False
For Each V In dctTrees.Keys
Set cT = dctTrees(V)
With cT
I = I + 1
rRes.Worksheet.Hyperlinks.Add _
Anchor:=rRes(I, .Level - lMinLevel + 1), _
Address:="File:///" & .FullPath, _
ScreenTip:=.FullPath, _
TextToDisplay:=.FolderName
For Each W In .Files.Keys
I = I + 1
rRes(I, .Level - lMinLevel + 2) = W
Next W
End With
Next V
Application.ScreenUpdating = True
End If
End Sub
输出:010101
希望它有所帮助。感谢。
答案 2 :(得分:0)
finally I managed to reverse the bits of a byte in PHP using the following algorithm:
$binary = decbin(ord($char));
$binary = str_pad($binary, 8, 0, STR_PAD_LEFT);
$binary = strrev($binary);
$reversednumber = bindec($binary);
$reversed = pack("C",$reversednumber);
first, convert the byte to an int and get the binary representation. Then it fills it with 0 until it's 8 chars long. then it reverses the string and assemble the string back to a number. The final step is to pack it as an one byte string again.