Jump to content

[SOLVED] bin2hex, pack()?


asmith

Recommended Posts

Hi,

I'm reading the content of some binary file and putting that into an array like this :
[code[
<?php
$location = 'file.csh';
$file_open = fopen($location, "rb");
$file_content = fread($file_open, filesize($location));
fclose($file_open);

$char_Hex = '';

for($i = 0; $i < strlen($file_content); $i++) 
{
   $char_Hex .= substr(bin2hex($contents{$i}),-2).'|';
}

$info_array =  explode('|',substr($char_Hex,0,-1));
?>

Now that I have the array, I do my changes in the array.

After the changes, I need to go back and save it the way the data was in that file.

 

Should I use pack()?

Can anyone tell me how can I go backward this process?

 

Link to comment
https://forums.phpfreaks.com/topic/141478-solved-bin2hex-pack/
Share on other sites

I used  :

 

<?php
$the_array[45] ='ff';  // The change i wanted to do in the data

$content_string = ''; // now write back the way it used to be
foreach($the_array as $content_char)
{
$content_string .= pack('a',$content_char); // I used all the formats like 's', 'h', 'n'), But non is giving me my result.
}
?>

 

Anyone?

 

 

Link to comment
https://forums.phpfreaks.com/topic/141478-solved-bin2hex-pack/#findComment-741417
Share on other sites

try this

 

what mod's you making ?

as their probably a better way

 

<?php
$location = 'file.csh';
$file_open = fopen($location, "rb");
$file_content = fread($file_open, filesize($location));
fclose($file_open);

$info_array = array();
$L = strlen($file_content);
for($i = 0; $i < $L; $i++) 
{
   $info_array[] = substr(bin2hex($contents{$i}),-2).'|';
}
//Do mod's

//whatever...

//rebuild
$bin_array = array();
foreach($info_array as $Hex)
{
   $bin_array[] =  hex2bin($Hex);
}
file_put_contents("newfile.csh",implode("",$bin_array));
?>

Link to comment
https://forums.phpfreaks.com/topic/141478-solved-bin2hex-pack/#findComment-742150
Share on other sites

Okay....

 

function hex2bin($h)
{
  if (!is_string($h)) return null;
  $r='';
  for ($a=0; $a<strlen($h); $a+=2) { $r.=chr(hexdec($h{$a}.$h{($a+1)})); }
  return $r;
}

 

their is now  ;D

 

EDIT: infact as were only doing one hex at a time you could use hexdec() by itself

any chance of letting on to what your changing ?

Link to comment
https://forums.phpfreaks.com/topic/141478-solved-bin2hex-pack/#findComment-742177
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.