asmith Posted January 19, 2009 Share Posted January 19, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/141478-solved-bin2hex-pack/ Share on other sites More sharing options...
asmith Posted January 20, 2009 Author Share Posted January 20, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/141478-solved-bin2hex-pack/#findComment-741417 Share on other sites More sharing options...
MadTechie Posted January 21, 2009 Share Posted January 21, 2009 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)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/141478-solved-bin2hex-pack/#findComment-742150 Share on other sites More sharing options...
asmith Posted January 21, 2009 Author Share Posted January 21, 2009 Thanks for the reply. There's no function called hex2bin, I get undefined function error by that. (That's where I got confused by the pack() ) Quote Link to comment https://forums.phpfreaks.com/topic/141478-solved-bin2hex-pack/#findComment-742156 Share on other sites More sharing options...
MadTechie Posted January 21, 2009 Share Posted January 21, 2009 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 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/141478-solved-bin2hex-pack/#findComment-742177 Share on other sites More sharing options...
asmith Posted January 21, 2009 Author Share Posted January 21, 2009 Man, It worked. I really really really don't know what to say to you. You came from nowhere today and fixed 2 of my problems. Don't know how to thank you ^^ Quote Link to comment https://forums.phpfreaks.com/topic/141478-solved-bin2hex-pack/#findComment-742201 Share on other sites More sharing options...
MadTechie Posted January 21, 2009 Share Posted January 21, 2009 Your very welcome, Topic Solved then Quote Link to comment https://forums.phpfreaks.com/topic/141478-solved-bin2hex-pack/#findComment-742205 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.