note 92953 added to function.hexdec
| From: | jose dot rob dot jr at gmail dot com | Date: | Sun, 16 Aug 2009 03:13:26 +0000 |
| Subject: | note 92953 added to function.hexdec | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159538@lists.php.net to get a copy of this message | ||
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
----
Server IP: 187.16.23.128
Probable Submitter: 189.93.255.237
----
Manual Page -- http://www.php.net/manual/en/function.hexdec.php
Edit -- https://master.php.net/note/edit/92953
Del: integrated -- https://master.php.net/note/delete/92953/integrated
Del: useless -- https://master.php.net/note/delete/92953/useless
Del: bad code -- https://master.php.net/note/delete/92953/bad+code
Del: spam -- https://master.php.net/note/delete/92953/spam
Del: non-english -- https://master.php.net/note/delete/92953/non-english
Del: in docs -- https://master.php.net/note/delete/92953/in+docs
Del: other reasons-- https://master.php.net/note/delete/92953
Reject -- https://master.php.net/note/reject/92953
Search -- https://master.php.net/manage/user-notes.php