Jump to content

[SOLVED] loop a binary file every 12 bytes


Recommended Posts

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

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

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'.

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?

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 :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.