dsaba Posted September 22, 2007 Share Posted September 22, 2007 does anyone know how to create a loop to get every 12 bytes of data from a file in binary? with file_get_contents() no luck searching for any such algorithim, and don't know the function... -thanks Quote Link to comment https://forums.phpfreaks.com/topic/70235-solved-loop-a-binary-file-every-12-bytes/ Share on other sites More sharing options...
MmmVomit Posted September 22, 2007 Share Posted September 22, 2007 You want to use fgetc() in a for loop. Quote Link to comment https://forums.phpfreaks.com/topic/70235-solved-loop-a-binary-file-every-12-bytes/#findComment-352754 Share on other sites More sharing options...
dsaba Posted September 22, 2007 Author Share Posted September 22, 2007 ok fgetc — Gets character from file pointer so it gets 1 characer file_get_contents() which is binary safe reads the entire file (all the characters) 1 byte is 8 bits, so is 1 character read from fgetc equal to 1 bit ?? normally ascii "characters" are more than 1 bit right? is the characters that it reads in bits or what? if this is true then i can just use str_split() which splits by "character" again, is that 1 bit?, then i can do it like that.... fopen() with 'r' places pointer at teh begg of the file, any way to place the pointer at the offset or start you choose like 8 bits or 8 "characters" in -thank u Quote Link to comment https://forums.phpfreaks.com/topic/70235-solved-loop-a-binary-file-every-12-bytes/#findComment-352760 Share on other sites More sharing options...
MmmVomit Posted September 22, 2007 Share Posted September 22, 2007 zz Quote Link to comment https://forums.phpfreaks.com/topic/70235-solved-loop-a-binary-file-every-12-bytes/#findComment-352763 Share on other sites More sharing options...
dsaba Posted September 22, 2007 Author Share Posted September 22, 2007 what i'm trying to do is exactly this: create an array of every 12 bytes of a file which is in binary seriously this is the goal I just read this: string file_get_contents ( string $filename [, int $flags [, resource $context [, int $offset [, int $maxlen]]]] ) so yeah u can specify an offset apparantly Quote Link to comment https://forums.phpfreaks.com/topic/70235-solved-loop-a-binary-file-every-12-bytes/#findComment-352766 Share on other sites More sharing options...
MmmVomit Posted September 22, 2007 Share Posted September 22, 2007 Scratch my earlier post. Here's the function you want. fread($your_file, 12); It will read in twelve bytes from the file. Quote Link to comment https://forums.phpfreaks.com/topic/70235-solved-loop-a-binary-file-every-12-bytes/#findComment-352770 Share on other sites More sharing options...
MmmVomit Posted September 22, 2007 Share Posted September 22, 2007 I just read this: string file_get_contents ( string $filename [, int $flags [, resource $context [, int $offset [, int $maxlen]]]] ) The offset won't get you what you want. If this is the file you're trying to read: abcdefghijklmnopqrstuv Calling file_get_contents with an offset of 5 will just get you 'fghijklmnopqrstuv'. Quote Link to comment https://forums.phpfreaks.com/topic/70235-solved-loop-a-binary-file-every-12-bytes/#findComment-352773 Share on other sites More sharing options...
dsaba Posted September 22, 2007 Author Share Posted September 22, 2007 ur awesome! out of curiousity and I will prolly need this later as well, if I have 1 character (1 byte), how can I find the numerical value? (a function) and whats the point? if all characters are 1 byte thats 8 bits, well i know that they are all 8 bits, all the same i know ord() finds the numerical ascii value and chr() returns the ascii value by the integer numerical value but what other "numerical" values are there for bytes/bits aside from ascii numerical values? Quote Link to comment https://forums.phpfreaks.com/topic/70235-solved-loop-a-binary-file-every-12-bytes/#findComment-352774 Share on other sites More sharing options...
dsaba Posted September 22, 2007 Author Share Posted September 22, 2007 ok this works great however, how do I know when to stop my loop for every 12 bytes, is there way to calculate total # of bytes in the resource handle? Quote Link to comment https://forums.phpfreaks.com/topic/70235-solved-loop-a-binary-file-every-12-bytes/#findComment-352778 Share on other sites More sharing options...
MmmVomit Posted September 22, 2007 Share Posted September 22, 2007 Try putting it as the clause of a while loop. It will probably return false when there's nothing left to read. That, and the PHP manual is your friend. Quote Link to comment https://forums.phpfreaks.com/topic/70235-solved-loop-a-binary-file-every-12-bytes/#findComment-352782 Share on other sites More sharing options...
dsaba Posted September 22, 2007 Author Share Posted September 22, 2007 best of friends! Here's my final code if anyone needs to know algorithim in the future: <?php $handle = fopen($Root.'/dir/otherDir/file.ext', 'r') or die('cannot open file'); $before = file_get_contents($Root.'/dir/otherDir/file.ext'); $length = strlen($before); $loopEnd = ($length/12) + 5; for ($i=0; $i<=$loopEnd; $i++) { $start = $i * 12; if ($start < $length) { if (($start + 12) > $length) { $num = $length - $start; } else { $num = 12; } fseek($handle, $start, SEEK_SET); $rArray[] = fread($handle, $num); //write2file($raw.'SPLIT', '/dir/otherdir', 'sampleFile', 'txt', 'a'); } else { continue; } } outputTextarea($rArray, '100', '60'); $after = implode('', $rArray); if ($before == $after) { echo "They are equal! and original length is: $length <br>"; } else { echo "not equal!<br>"; } ?> fseek() seeks to a certain offset to begin reading, and sets the pointer fread() reads so many bytes past the pointer I counted the original file bytes and then the imploded bytes read from the loop, and they equal. It also takes into account if the last loop does not have exactly 12 bytes left to read. Enjoy -thanks for all your help everyone Quote Link to comment https://forums.phpfreaks.com/topic/70235-solved-loop-a-binary-file-every-12-bytes/#findComment-352785 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.