Wednesday, December 17, 2003

Byte arrays in PHP

Byte arrays are represented in PHP as "binary strings". PHP is perfectly capable of doing sophisticated bit-mapped operations, yet the documentation I've read is far from clear as to the methods. I'll hopefully save you some time and effort: here's a way to create a binary string from a series of hex values:

$bs = pack("C*", 0x41, 0x44, 0x45); // pack unsigned chars, yields "ADE"
$bs = pack("n*", 0x8000, 0x8001, 0xFE5A); // pack unsigned words
$bs = pack("N*", 0x70018001, 0x80042E5A); // pack unsigned double-words


Okay, that was easy. The unpacking of a binary string into separate values is a little more obtuse:

$bs = pack("CnN", 0x41, 0x8001, 0x80000001);
$aTemp = unpack("Cmyunsignedchar/nmyunsignedword/Nmyunsigneddw", $bs);


After execution of this command, $aTemp["myunsignedchar"] is 0x41, $aTemp["myunsignedword"] is 0x8001, $aTemp["myunsigneddw"] is 0x80000001. The unpack's first argument is a series of data-types, followed immediately by the keys used in the returned associative array for storage of the unpacked values. If that explanation doesn't suffice, see some excellent Unpack Examples.

Announcing the WorldWideWeb

This is pretty neat... Tim Berners-Lee's original announcement of the web.

Reacher

If you've never read the books of Lee Child, you are missing one of the best action/adventure authors of all-time. Only C.S. Forester, A.J. Quinnell and Raymond Chandler compare. Seriously. All of the books track the experiences of Jack Reacher, formerly of the U.S. Military Police, as he travels the country enjoying a vagabond-style existence. The most recent book in the series, Persuader, was one of the finest books I've ever read.



Dallas

My wife reminds me that Linda Gray was in Dallas ("Sue Ellen, you idiot"), not Dynasty. Anyhow, having seen Linda naked, I can only muse about one of the all-time great female actresses, Anne Bancroft.


No comments:

Post a Comment