我期待已久的一種方式來查看文件用十六進制前一段時間,發現:PHP查看文件爲十六進制?
class Hex
{
var $file;
var $hex;
function __construct($file)
{
$this->file = $file;
}
function gethex()
{
$handle = fopen($this->file, 'r') or die('Permission?');
while(!feof($handle))
{
foreach(unpack('C*',fgets($handle)) as $dec)
{
$tmp = dechex($dec);
$this->hex[] .= strtoupper(str_repeat('0',2-strlen($tmp)).$tmp);
}
}
return join($this->hex);
}
function writehex($hexcode)
{
foreach(str_split($hexcode,2) as $hex)
{
$tmp .= pack('C*', hexdec($hex));
}
$handle = fopen($this->file, 'w+') or die('Permission?');
fwrite($handle, $tmp);
}
}
它爲一個文件偉大的工作,但我覺得我遇到了問題,試圖多辦呢文件。腳本有什麼問題嗎?它應該關閉文件的某個地方嗎?我應該在使用它們之後刪除它的實例嗎?
這會是更好的?:
class Hex
{
var $file;
var $hex;
function __construct($file)
{
$this->file = $file;
}
function gethex()
{
$handle = fopen($this->file, 'r') or die('Permission?');
while(!feof($handle))
{
foreach(unpack('C*',fgets($handle)) as $dec)
{
$tmp = dechex($dec);
$this->hex[] .= strtoupper(str_repeat('0',2-strlen($tmp)).$tmp);
}
}
fclose($handle);
return join($this->hex);
}
function writehex($hexcode)
{
foreach(str_split($hexcode,2) as $hex)
{
$tmp .= pack('C*', hexdec($hex));
}
$handle = fopen($this->file, 'w+') or die('Permission?');
fwrite($handle, $tmp);
fclose($handle);
}
}
爲什麼不乾脆使用二進制模式? – Pwnna
http://php.net/manual/en/function.bin2hex.php –
對於這一切,我還是一個小菜鳥,我習慣在HxD中離線編輯文件。稍後我可能會嘗試更改它 – mowwwalker