note 92953 modified in function.hexdec by danbrown

From: Date: Thu, 06 May 2010 15:47:35 +0000
Subject: note 92953 modified in function.hexdec by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-169018@lists.php.net to get a copy of this message
I made these functions to pack up to 64 ID's into a mysql unsigned bigint. ID's cannot repeat, must be <= bit's limit and > 0. The functions uses php 32 bit's int as unsigned because we don't actually read the number, just the bits. Then 0xFFFFFFFF display -1 but the bit's are there (tested with linux 2.6 i686 and x86_64) --- This is an other way to make hex to bin conversions: <?php function hexbin($hex, $padding = false) { // Validation $hex = preg_replace('/^(0x|X)?/i', '', $hex); $hex = preg_replace('/[[:blank:]]/', '', $hex); if(empty($hex)) { $hex = '0'; } if(!preg_match('/^[0-9A-F]*$/i', $hex)) { trigger_error('Argument is not a hex', E_USER_WARNING); return false; } // Conversion $bin = ''; $hex = array_reverse(str_split($hex)); foreach($hex as $n) { $n = hexdec($n); for($i = 1; $i <= 8; $i <<= 1) { $bin .= ($i & $n)? '1' : '0'; } if($padding) { $bin .= ' '; } } return ltrim(strrev($bin)); } // Tests echo "<b>Debug:</b> <pre>"; // Randomly choosed padded number var_dump(hexbin('00FF FF8F 7F3F FF1F', true)); // string(79) "0000 0000 1111 1111 1111 1111 1000 1111 0111 1111 0011 1111 1111 1111 0001 1111" // Yellow RGB var_dump(hexbin('0xF8F800')); // string(24) "111110001111100000000000" // Green RGB (padded) var_dump(hexbin('0x008800', true)); //string(29) "0000 0000 1000 1000 0000 0000" die("\n<br>debug"); ?> Have fun ;D --was-- I've messed up a thing on my last message... Replace: $hex = preg_replace('/\b/', '', $hex); By: $hex = preg_replace('/[[:blank:]]', '', $hex); If you don't do it, spaces will not be removed and you may have problems if you send a padded hex string --- This is an other way to make hex to bin conversions: <?php function hexbin($hex, $padding = false) { // Validation $hex = preg_replace('/^(0x|X)?/i', '', $hex); $hex = preg_replace('/[[:blank:]]/', '', $hex); if(empty($hex)) { $hex = '0'; } if(!preg_match('/^[0-9A-F]*$/i', $hex)) { trigger_error('Argument is not a hex', E_USER_WARNING); return false; } // Conversion $bin = ''; $hex = array_reverse(str_split($hex)); foreach($hex as $n) { $n = hexdec($n); for($i = 1; $i <= 8; $i <<= 1) { $bin .= ($i & $n)? '1' : '0'; } if($padding) { $bin .= ' '; } } return ltrim(strrev($bin)); } // Tests echo "<b>Debug:</b> <pre>"; // Randomly choosed padded number var_dump(hexbin('00FF FF8F 7F3F FF1F', true)); // string(79) "0000 0000 1111 1111 1111 1111 1000 1111 0111 1111 0011 1111 1111 1111 0001 1111" // Yellow RGB var_dump(hexbin('0xF8F800')); // string(24) "111110001111100000000000" // Green RGB (padded) var_dump(hexbin('0x008800', true)); //string(29) "0000 0000 1000 1000 0000 0000" die("\n<br>debug"); ?> Have fun ;D http://php.net/manual/en/function.hexdec.php

« previous php.notes (#169018) next »