chx
chx

Reputation: 11760

How to convert "32 bit" to number in PHP?

I am printing the number of rows in a DBF file and this works:

$content = fread($fp, 8);
$number = 0;
for ($i = 7; $i >= 4; $i--) {
  $number = $number * 256 + ord($content[$i]);
}
print $number;

But I am reasonably sure there is a pack or unpack command which would do the same. Is there?

Upvotes: 0

Views: 81

Answers (1)

Alex Howansky
Alex Howansky

Reputation: 53573

It looks like $content contains an 8 byte string and you want to ignore the first 4 bytes and take the last 4 bytes as an unsigned int. If that's the case, you can use:

$number = unpack('L', substr($content, 4))[1];

Upvotes: 2

Related Questions