I opened a file (binary), and then used the Bin2Hex function, and changed it into a 16-character string, and modified one of the values in a particular location, and now the problem is, I want to save back, found to be an ASCII file, how should you save the same code as the source file format?
Here's the code:
$FP =fopen ("Sample.bin", ' RB ');
$word = ";
while (!feof ($fp)) {
$buf = Bin2Hex (Fread ($FP, 48));
$pos = Strpos ($buf, ' 02000100 '); Find Land
If $pos = = 0 {
echo "This is the land";
//}
Echo $buf;
echo "\ n-------------\ n";
Echo ($pos/2);
echo $buf 2=str_replace ("02000100", "BE010000", $buf);
$word = $word. $buf 2;
echo "\ n";
}
Fclose ($FP);
Echo $word;
$new = fopen (' New.bin ', ' W ');
Fwrite ($new, $word);
Fclose ($new);
?>
Reply to discussion (solution)
A file format, save two formats also meaningless, can be removed when needed to convert, not to hexadecimal
1, since you use $fp =fopen ("Sample.bin", ' RB '); Open the file, indicate that your program can work under the window system, then write should also $new = fopen (' New.bin ', ' W B ');
b means binary mode, window must be strictly different under the Linux does not matter
2, the content of the processed $word is hexadecimal representation, you write to the file is not back to the binary
Available $word = Pack (' h* ', $word); Transformation
$word = ";
$from = Pack (' h* ', ' 02000100 ');
$target = Pack (' h* ', ' BE010000 ');
while (!feof ($fp)) {
$buf = Bin2Hex (Fread ($FP, 48));
$buf 2 = Str_replace ($from, $target, $buf);
$word. = $buf 2;
}
This is still binary data in $word
$word = ";
$from = Pack (' h* ', ' 02000100 ');
$target = Pack (' h* ', ' BE010000 ');
while (!feof ($fp)) {
$buf = Bin2Hex (Fread ($FP, 48));
$buf 2 = Str_replace ($from, $target, $buf);
$word. = $buf 2;
......
Thanks to Xuzuning's advice, I followed your revised code as follows:
$FP =fopen ("Old.bin", ' RB ');
$word = ";
$from = Pack (' h* ', ' 02000100 ');
$target = Pack (' h* ', ' BE010000 ');
while (!feof ($fp)) {
$buf = Bin2Hex (Fread ($FP, 48));
$buf 2 = Str_replace ($from, $target, $buf);
$pos = Strpos ($buf, ' 02000100 '); Find Land
If $pos = = 0 {
echo "This is the land";
//}
Echo $buf;
echo "\ n-------------\ n";
Echo ($pos/2);
echo $buf 2;
echo $buf 2=str_replace ("02000100", "BE010000", $buf);
$word = $word. $buf 2;
echo "\ n";
}
Fclose ($FP);
Echo $word;
$new = fopen (' New.bin ', ' WB ');
Fwrite ($new, $word);
Fclose ($new);
?>
But the size of the generated new.bin is still twice times old.bin, and there is no difference, my test environment is on my WIN2008 server, iis7+php, do you have any suggestions?
Sorry, I #3 the code is wrong, it should be so
$word = ";
$from = Pack (' h* ', ' 02000100 ');
$target = Pack (' h* ', ' BE010000 ');
while (!feof ($fp)) {
$buf = Fread ($fp, 48);
$buf 2 = Str_replace ($from, $target, $buf);
$word. = $buf 2;
}
Operate completely in binary mode
Sorry, I #3 the code is wrong, it should be so
$word = ";
$from = Pack (' h* ', ' 02000100 ');
$target = Pack (' h* ', ' BE010000 ');
while (!feof ($fp)) {
$buf = Fread ($fp, 48);
$buf 2 = Str_replace ($from, $target, $buf);
$wo ...
Thanks, problem solving!
Nagging said the key, Windows does distinguish between text and binary files, because the end of the text is marked with a special byte.
Under Windows, students who do not quite understand often use the FGETC function to open binary files with text, attempting a byte-by-byte read, and find that only half of the read begins to return EOF, in fact because a byte is a special byte that is the Terminator in the text.
But Linux will return 0 as the end of the file with the system Api:read, so the b parameter is ignored.
Landlord's practice is superfluous, not just want to find the land two words, and then replace it?
Do not know the landlord said the binary file is how to generate, in fact, everything is binary, just under Windows B open and non-B open results will be different, non-B open and edit the results with a text editor is consistent.
Suppose your original file is a UTF8 text file, there is "land" two words, the method is very simple, UTF8 multi-byte encoding, loop fread does not necessarily just can be the full "land" two words of bytes read into a buffer, so the better way is file_get_ Contents, then direct str_replace ("Land", "brother") i.e., in C language is actually a memory-mapped replacement string.
Turn what hex, purely superfluous, and the cyclic reading is certain to have the bug, possibly "the land" two words is to be opened to read in.
Even vim this software is memory-mapped, the file is too large it can not be able to tell you that failure.
How can I call the Save window to save?