Skip to content

Instantly share code, notes, and snippets.

@andriesss
Created September 20, 2012 14:50
Show Gist options
  • Save andriesss/3756380 to your computer and use it in GitHub Desktop.
Save andriesss/3756380 to your computer and use it in GitHub Desktop.
packing hex into binary, checking if bits are properly set
<?php
$bin = pack('C*', 0xFF, 0xFA, 0x2C, 0x32, 0xFF, 0xF0, 0xFF);
$hex = unpack('H*', $bin);
$hex = $hex[1];
echo "\nbinary: ";
$parts = explode(' ', chunk_split($hex, 2, ' '));
foreach ($parts as $part) {
if ($part) printf('%1$08s ', base_convert($part, 16, 2));
}
echo "\nhex: ";
foreach ($parts as $part) {
if ($part) printf(' 0x%s ', $part);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment