<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[沧海一粟]]></title> 
<link>http://www.dzhope.com/index.php</link> 
<description><![CDATA[Web系统架构与服务器运维,php开发]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[沧海一粟]]></copyright>
<item>
<link>http://www.dzhope.com/post//</link>
<title><![CDATA[php将16进制转换成10进制的函数hexdec]]></title> 
<author>jed &lt;jed521@163.com&gt;</author>
<category><![CDATA[代码编程]]></category>
<pubDate>Mon, 02 Oct 2006 08:58:34 +0000</pubDate> 
<guid>http://www.dzhope.com/post//</guid> 
<description>
<![CDATA[ 
	hexdec<br/>(PHP 3, PHP 4, PHP 5)<br/><br/>hexdec -- 十六进制转换为十进制<br/>说明<br/>number hexdec ( string hex_string )<br/><br/><br/>返回与 hex_string 参数所表示的十六进制数等值的的十进制数。hexdec() 将一个十六进制字符串转换为十进制数。所能转换的最大数值为 7fffffff，即十进制的 2147483647。PHP 4.1.0 开始，该函数可以处理大数字，这种情况下，它会返回 float 类型。 <br/><br/>hexdec() 将遇到的所有非十六进制字符替换成 0。这样，所有左边的零都被忽略，但右边的零会计入值中。 例子 1. hexdec() 例子<br/><br/>&lt;?php<br/>var_dump(hexdec(&quot;See&quot;));<br/>var_dump(hexdec(&quot;ee&quot;));<br/>// both print &quot;int(238)&quot;<br/><br/>var_dump(hexdec(&quot;that&quot;)); // print &quot;int(10)&quot;<br/>var_dump(hexdec(&quot;a0&quot;)); // print &quot;int(160)&quot;<br/>?&gt; &nbsp;<br/> <br/><br/><br/>参见 dechex()，bindec()，octdec() 和base_convert()。 <br/><br/><br/><br/> add a note User Contributed Notes<br/><br/>ayadav at infoprocorp dot com<br/>28-Dec-2005 04:03 <br/>From Amit Yadav<br/><br/>Hex to binary conversion<br/><br/>$num = hexdec(&quot;20DF&quot;);<br/>echo binfromdec($num);<br/><br/>function binfromdec($num) <br/>{<br/> &nbsp; if ($num &gt; 32766) &nbsp; &nbsp;return (&quot;Too Large!&quot;);<br/> &nbsp; if ($num &amp; 16384) &nbsp; &nbsp;$bit15 = 1;<br/> &nbsp; if ($num &amp; 8192) &nbsp; &nbsp;$bit14 = 1;<br/> &nbsp; if ($num &amp; 4096) &nbsp; &nbsp;$bit13 = 1;<br/> &nbsp; if ($num &amp; 2048) &nbsp; &nbsp;$bit12 = 1;<br/> &nbsp; if ($num &amp; 1024) &nbsp; &nbsp;$bit11 = 1;<br/> &nbsp; if ($num &amp; 512) &nbsp; &nbsp; &nbsp; &nbsp;$bit10 = 1;<br/> &nbsp; if ($num &amp; 256) &nbsp; &nbsp; &nbsp; &nbsp;$bit9 = 1;<br/> &nbsp; if ($num &amp; 128) &nbsp; &nbsp; &nbsp; &nbsp;$bit8 = 1;<br/> &nbsp; if ($num &amp; 64) &nbsp; &nbsp; &nbsp; &nbsp;$bit7 = 1;<br/> &nbsp; if ($num &amp; 32) &nbsp; &nbsp; &nbsp; &nbsp;$bit6 = 1;<br/> &nbsp; if ($num &amp; 16) &nbsp; &nbsp; &nbsp; &nbsp;$bit5 = 1;<br/> &nbsp; if ($num &amp; 8) &nbsp; &nbsp; &nbsp; &nbsp;$bit4 = 1;<br/> &nbsp; if ($num &amp; 4) &nbsp; &nbsp; &nbsp; &nbsp;$bit3 = 1;<br/> &nbsp; if ($num &amp; 2) &nbsp; &nbsp; &nbsp; &nbsp;$bit2 = 1;<br/> &nbsp; if ($num &amp; 1) &nbsp; &nbsp; &nbsp; &nbsp;$bit1 = 1;<br/><br/> &nbsp; return (&quot;&quot; . $bit15 . $bit14 . $bit13 . $bit12 . $bit11 . $bit10 . $bit9 . $bit8 . $bit7 . $bit6 . $bit5 . $bit4 . $bit3 . $bit2 . $bit1);<br/><br/>} <br/>cory at lavacube dot com<br/>29-Oct-2005 04:59 <br/>A handy little function to convert HEX colour codes to &quot;web safe&quot; colours...<br/><br/>&lt;?php<br/><br/>function color_mkwebsafe ( $in )<br/>{<br/> &nbsp; // put values into an easy-to-use array<br/> &nbsp; $vals[&#039;r&#039;] = hexdec( substr($in, 0, 2) );<br/> &nbsp; $vals[&#039;g&#039;] = hexdec( substr($in, 2, 2) );<br/> &nbsp; $vals[&#039;b&#039;] = hexdec( substr($in, 4, 2) );<br/><br/> &nbsp; // loop through<br/> &nbsp; foreach( $vals as $val )<br/> &nbsp; {<br/> &nbsp; &nbsp; &nbsp; // convert value<br/> &nbsp; &nbsp; &nbsp; $val = ( round($val/51) * 51 );<br/> &nbsp; &nbsp; &nbsp; // convert to HEX<br/> &nbsp; &nbsp; &nbsp; $out .= str_pad(dechex($val), 2, &#039;0&#039;, STR_PAD_LEFT);<br/> &nbsp; }<br/><br/> &nbsp; return $out;<br/>}<br/><br/>?&gt;<br/><br/>Example: color_mkwebsafe(&#039;0e5c94&#039;);<br/>Produces: 006699<br/><br/>Hope this helps someone out... Happy coding. :-) <br/>hajamie@home<br/>24-Oct-2005 10:38 <br/>This function will take a hex $color, e.g. fa34bc and make it the shade you want, e.g. anywhere between 0 and 255. The smaller the number, the darker the output color. &nbsp;It keeps the color the same, only changing the shade. &nbsp;<br/><br/>function correctshade($color, $shade)<br/>{<br/>$r = hexdec(substr($color,0,2));<br/>$g = hexdec(substr($color,2,2));<br/>$b = hexdec(substr($color,4,2));<br/>$sum = ($r + $g + $b);<br/>$x = (($shade * 3) - $sum) / $sum;<br/>if ($x &gt;= 0) {<br/>$x = $x + 1;<br/>} else {<br/>$x = 1 + $x;<br/>}<br/>$r = intval($x * $r);<br/>$g = intval($x * $g);<br/>$b = intval($x * $b);<br/>$r = dechex($r);<br/>$g = dechex($g);<br/>$b = dechex($b);<br/>return $r.$g.$b;<br/>} <br/>09-Oct-2005 01:53 <br/>I needed to get the opposite colors so my website would be soft on the eyes.<br/><br/>&lt;?php<br/>function OppositeHex($color)<br/>{<br/>$r = dechex(255 - hexdec(substr($color,0,2)));<br/>$r = (strlen($r) &gt; 1) ? $r : &#039;0&#039;.$r;<br/>$g = dechex(255 - hexdec(substr($color,2,2)));<br/>$g = (strlen($g) &gt; 1) ? $g : &#039;0&#039;.$g;<br/>$b = dechex(255 - hexdec(substr($color,4,2)));<br/>$b = (strlen($b) &gt; 1) ? $b : &#039;0&#039;.$b;<br/>return $r.$g.$b;<br/>}<br/><br/>//Example<br/>$color = &#039;000000&#039;;<br/>echo &#039;The opposite of #&#039;.$color.&#039; is #&#039;.OppositeHex($color).&#039;;<br/>?&gt; <br/>detrate at hotmail dot com<br/>30-Sep-2005 09:38 <br/>I made this for a little phpbb mod. &nbsp;It was used to take the hex value from the database and make a color 20 (in decimal) less, resulting a darker color.<br/><br/>EXAMPLE: #336699 to #1f5285<br/><br/>&lt;?php<br/><br/>$row1 = &quot;336699&quot;; // color<br/>$c = 20; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// difference value<br/><br/>$rgb = array(substr($row1,0,2), substr($row1,2,2), substr($row1,4,2));<br/><br/>for($i=0; $i &lt; 3; $i++)<br/>{<br/> &nbsp;if((hexdec($rgb[$i])-$c) &gt;= 0)<br/> &nbsp;{<br/> &nbsp; $rgb[$i] = hexdec($rgb[$i])-$c;<br/><br/> &nbsp; $rgb[$i] = dechex($rgb[$i]);<br/> &nbsp; if(hexdec($rgb[0]) &lt;= 9)<br/> &nbsp; &nbsp; $rgb[$i] = &quot;0&quot;.$rgb[$i];<br/> &nbsp;} else {<br/> &nbsp; $rgb[$i] = &quot;00&quot;;<br/> &nbsp;}<br/>}<br/><br/>$row2 = $rgb[0].$rgb[1].$rgb[2];<br/><br/>?&gt; <br/>Gabriel Reguly<br/>17-Aug-2005 02:24 <br/>After esnhexdec from &quot;rledger at gmail dot com&quot;, &nbsp;the esndechex:<br/><br/>&lt;?php<br/> &nbsp; function esndechex($dec){<br/> &nbsp; &nbsp; &nbsp; &nbsp; $a = strtoupper(dechex(substr($dec, 1, &nbsp;2)));<br/> &nbsp; &nbsp; &nbsp; &nbsp; $b = strtoupper(dechex(substr($dec, 3, 10)));<br/> &nbsp; &nbsp; &nbsp; &nbsp; return $a . $b;<br/> &nbsp; }<br/>?&gt; <br/>djneoform at gmail dot com<br/>21-Jun-2005 12:51 <br/>since i couldn&#039;t find one, here&#039;s an HEX to ASCII converter..<br/><br/>(takes HEX strings (in ASCII) and converts each two digit HEX code into it&#039;s ASCII equivalent)<br/><br/>function hex2ascii($str)<br/>{<br/> &nbsp; $p = &#039;&#039;;<br/> &nbsp; for ($i=0; $i &lt; strlen($str); $i=$i+2)<br/> &nbsp; {<br/> &nbsp; &nbsp; &nbsp; $p .= chr(hexdec(substr($str, $i, 2)));<br/> &nbsp; }<br/> &nbsp; return $p;<br/>} <br/>bishop<br/>05-Jun-2005 10:51 <br/>Bullet-proof hex-to-rgb colour converter like brian at sagesport dot com wanted, just far fewer code lines. As a bonus, gives you the ability to return as string or array:<br/><br/>&lt;?php<br/> &nbsp; function &amp;hex2rgb($hex, $asString = true) <br/> &nbsp; {<br/> &nbsp; &nbsp; &nbsp; // strip off any leading #<br/> &nbsp; &nbsp; &nbsp; if (0 === strpos($hex, &#039;#&#039;)) { <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $hex = substr($hex, 1);<br/> &nbsp; &nbsp; &nbsp; } else if (0 === strpos($hex, &#039;&amp;H&#039;)) {<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $hex = substr($hex, 2);<br/> &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp;<br/><br/> &nbsp; &nbsp; &nbsp; // break into hex 3-tuple<br/> &nbsp; &nbsp; &nbsp; $cutpoint = ceil(strlen($hex) / 2)-1; <br/> &nbsp; &nbsp; &nbsp; $rgb = explode(&#039;:&#039;, wordwrap($hex, $cutpoint, &#039;:&#039;, $cutpoint), 3);<br/><br/> &nbsp; &nbsp; &nbsp; // convert each tuple to decimal<br/> &nbsp; &nbsp; &nbsp; $rgb[0] = (isset($rgb[0]) ? hexdec($rgb[0]) : 0);<br/> &nbsp; &nbsp; &nbsp; $rgb[1] = (isset($rgb[1]) ? hexdec($rgb[1]) : 0);<br/> &nbsp; &nbsp; &nbsp; $rgb[2] = (isset($rgb[2]) ? hexdec($rgb[2]) : 0);<br/><br/> &nbsp; &nbsp; &nbsp; return ($asString ? &quot;{$rgb[0]} {$rgb[1]} {$rgb[2]}&quot; : $rgb);<br/> &nbsp; }<br/>?&gt;<br/><br/>Handles 2, 3, and 6 character colour codes with leading # or &amp;H. <br/>Joey Morwick<br/>03-May-2005 04:07 <br/>I found it helpful to have the inverse / reverse of this function laying around, since I wanted to insert some binary data into an xmlrpc call (it currently craps out when you do that), and I couldn&#039;t find one laying around, so here&#039;s a simple little function to do that:<br/><br/>function hex2bin($str) {<br/> &nbsp; $build = &#039;&#039;;<br/> &nbsp; while(strlen($str) &gt; 1) {<br/> &nbsp; &nbsp; &nbsp; $build .= chr(hexdec(substr($str, 0, 2)));<br/> &nbsp; &nbsp; &nbsp; $str = substr($str, 2, strlen($str)-2);<br/> &nbsp; }<br/> &nbsp; return $build;<br/>} <br/>rledger at gmail dot com<br/>29-Mar-2005 03:59 <br/>To convert a cellular ESN from hexadecimal to decimal, use the following code. The base conversion is different due to the fact that the first two characters of a hexadecimal value must be converted seperately from the remaining six characters.<br/>&lt;?<br/>function esnhexdec($hex){<br/> &nbsp; $a = sprintf(&quot;%03d&quot;, hexdec(substr($hex, 0, 2)));<br/> &nbsp; $b = sprintf(&quot;%08d&quot;, hexdec(substr($hex, 2, 6)));<br/> &nbsp; return $a . $b;<br/>}<br/>?&gt; <br/>zubfatal, root at it dot dk<br/>25-Mar-2005 06:36 <br/>This replaces my previous class.<br/>I&#039;ve added a few more input checks in the rgb2hex function.<br/>Also it returned incorrect hex values for 1-digit values.<br/><br/>color::rgb2hex(array(0,0,0)) would output 000 not 00000.<br/><br/>&lt;?php <br/><br/>/** <br/>* Convert colors <br/>* <br/>* Usage: <br/>* &nbsp;color::hex2rgb(&quot;FFFFFF&quot;) <br/>* &nbsp;color::rgb2hex(array(171,37,37)) <br/>* <br/>* @author &nbsp; &nbsp; &nbsp;Tim Johannessen &lt;root@it.dk&gt; <br/>* @version &nbsp; &nbsp;1.0.1 <br/>*/ <br/><br/>class color { <br/><br/> &nbsp; /** <br/> &nbsp; &nbsp; * Convert HEX colorcode to an array of colors. <br/> &nbsp; &nbsp; * @return &nbsp; &nbsp; &nbsp;array &nbsp; &nbsp; &nbsp; &nbsp;Returns the array of colors as array(red,green,blue) <br/> &nbsp; &nbsp; */ <br/> &nbsp; &nbsp; <br/> &nbsp; function hex2rgb($hexVal = &quot;&quot;) { <br/> &nbsp; &nbsp; &nbsp; $hexVal = eregi_replace(&quot;[^a-fA-F0-9]&quot;, &quot;&quot;, $hexVal); <br/> &nbsp; &nbsp; &nbsp; if (strlen($hexVal) != 6) { return &quot;ERR: Incorrect colorcode, expecting 6 chars (a-f, 0-9)&quot;; } <br/> &nbsp; &nbsp; &nbsp; $arrTmp = explode(&quot; &quot;, chunk_split($hexVal, 2, &quot; &quot;)); <br/> &nbsp; &nbsp; &nbsp; $arrTmp = array_map(&quot;hexdec&quot;, $arrTmp); <br/> &nbsp; &nbsp; &nbsp; return array(&quot;red&quot; =&gt; $arrTmp[0], &quot;green&quot; =&gt; $arrTmp[1], &quot;blue&quot; =&gt; $arrTmp[2]); <br/> &nbsp; } <br/> &nbsp; &nbsp; <br/> &nbsp; /** <br/> &nbsp; &nbsp; * Convert RGB colors to HEX colorcode <br/> &nbsp; &nbsp; * @return &nbsp; &nbsp; &nbsp;string &nbsp; &nbsp; &nbsp; &nbsp;Returns the converted colors as a 6 digit colorcode <br/> &nbsp; &nbsp; */ <br/> &nbsp; function rgb2hex($arrColors = null) { <br/> &nbsp; &nbsp; &nbsp; if (!is_array($arrColors)) { return &quot;ERR: Invalid input, expecting an array of colors&quot;; } <br/> &nbsp; &nbsp; &nbsp; if (count($arrColors) &lt; 3) { return &quot;ERR: Invalid input, array too small (3)&quot;; } <br/> &nbsp; &nbsp; &nbsp; &nbsp; <br/> &nbsp; &nbsp; &nbsp; array_splice($arrColors, 3); <br/> &nbsp; &nbsp; &nbsp; &nbsp; <br/> &nbsp; &nbsp; &nbsp; for ($x = 0; $x &lt; count($arrColors); $x++) { <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (strlen($arrColors[$x]) &lt; 1) { <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return &quot;ERR: One or more empty values found, expecting array with 3 values&quot;; <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif (eregi(&quot;[^0-9]&quot;, $arrColors[$x])) { <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return &quot;ERR: One or more non-numeric values found.&quot;; <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else { <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((intval($arrColors[$x]) &lt; 0) &#124;&#124; (intval($arrColors[$x]) &gt; 255)) { <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return &quot;ERR: Range mismatch in one or more values (0-255)&quot;; <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else { <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $arrColors[$x] = strtoupper(str_pad(dechex($arrColors[$x]), 2, 0, STR_PAD_LEFT)); <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } <br/> &nbsp; &nbsp; &nbsp; } <br/> &nbsp; &nbsp; &nbsp; &nbsp; <br/> &nbsp; &nbsp; &nbsp; return implode(&quot;&quot;, $arrColors); <br/> &nbsp; } <br/><br/>} <br/><br/>?&gt; <br/>brian at sagesport dot com<br/>23-Mar-2005 01:08 <br/>The issue I&#039;ve seen with the existing hex to dec conversion routines is the lack of error-trapping. &nbsp;I stick to the theory that one should try to cover ALL the bases when writing a generalized routine such as this one. &nbsp;I have a varied background that covers a wide variety of design/development languages, on the web as well as desktop apps. &nbsp;As such I&#039;ve seen multiple formats for writing hex colors.<br/><br/>For example, the color red COULD be written as follows:<br/>#ff0000<br/>&amp;Hff0000<br/>#ff<br/>&amp;Hff<br/><br/>Therefore I have written a function that is case-insensitive and takes into account the chance that different developers have a tendency to format hex colors in different ways.<br/><br/>&lt;?php<br/> &nbsp;function convert_color($hex){<br/> &nbsp; $len = strlen($hex);<br/> &nbsp; $chars = array(&quot;#&quot;,&quot;&amp;&quot;,&quot;H&quot;,&quot;h&quot;);<br/> &nbsp; $hex = strip_chars($hex, $chars);<br/> &nbsp; preg_match(&quot;/([0-9]&#124;[A-F]&#124;[a-f]){&quot;.$len.&quot;}/i&quot;,$hex,$arr);<br/> &nbsp; $hex = $arr[0];<br/> &nbsp; if ($hex) {<br/> &nbsp; &nbsp; switch($len) {<br/> &nbsp; &nbsp; &nbsp; case 2:<br/> &nbsp; &nbsp; &nbsp; &nbsp; $red = hexdec($hex);<br/> &nbsp; &nbsp; &nbsp; &nbsp; $green = 0;<br/> &nbsp; &nbsp; &nbsp; &nbsp; $blue = 0;<br/> &nbsp; &nbsp; &nbsp; &nbsp; break;<br/> &nbsp; &nbsp; &nbsp; case 4:<br/> &nbsp; &nbsp; &nbsp; &nbsp; $red = hexdec(substr($hex,0,2));<br/> &nbsp; &nbsp; &nbsp; &nbsp; $green=hexdec(substr($hex,2,2));<br/> &nbsp; &nbsp; &nbsp; &nbsp; $blue = 0;<br/> &nbsp; &nbsp; &nbsp; &nbsp; break;<br/> &nbsp; &nbsp; &nbsp; case 6:<br/> &nbsp; &nbsp; &nbsp; &nbsp; $red = hexdec(substr($hex,0,2));<br/> &nbsp; &nbsp; &nbsp; &nbsp; $green=hexdec(substr($hex,2,2));<br/> &nbsp; &nbsp; &nbsp; &nbsp; $blue = hexdec(substr($hex,4,2));<br/> &nbsp; &nbsp; &nbsp; &nbsp; break; &nbsp; &nbsp; &nbsp;<br/> &nbsp; &nbsp; };<br/> &nbsp; &nbsp; $color[success] = true;<br/> &nbsp; &nbsp; $color[r] = $red;<br/> &nbsp; &nbsp; $color[g] = $green;<br/> &nbsp; &nbsp; $color[b] = $blue;<br/> &nbsp; &nbsp; return $color;<br/> &nbsp; } else {<br/> &nbsp; &nbsp; $color[success] = false;<br/> &nbsp; &nbsp; $color[error] = &quot;unable to convert hex to dec&quot;;<br/> &nbsp; };<br/> &nbsp;}<br/><br/> &nbsp;function strip_chars($string, $char){<br/> &nbsp; $len = strlen($string);<br/> &nbsp; $count = count($char);<br/> &nbsp; if ($count &gt;= 2) {<br/> &nbsp; &nbsp; for ($i=0;$i&lt;=$count;$i++) {<br/> &nbsp; &nbsp; &nbsp; if ($char[$i]) {<br/> &nbsp; &nbsp; &nbsp; &nbsp; $found = stristr($string,$char[$i]);<br/> &nbsp; &nbsp; &nbsp; &nbsp; if ($found) {<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $val = substr($string,$found+1,$len-1);<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $string = $val;<br/> &nbsp; &nbsp; &nbsp; &nbsp; };<br/> &nbsp; &nbsp; &nbsp; };<br/> &nbsp; &nbsp; };<br/> &nbsp; } else {<br/> &nbsp; &nbsp; $found = stristr($string,$char);<br/> &nbsp; &nbsp; if ($found) {<br/> &nbsp; &nbsp; &nbsp; $val = substr($string,$found+1,$len-1);<br/> &nbsp; &nbsp; };<br/> &nbsp; };<br/> &nbsp; echo $val;<br/> &nbsp; return $val;<br/> &nbsp;}<br/><br/> &nbsp;/* <br/> &nbsp; To use simply use the following function call:<br/> &nbsp; &nbsp; $color = convert_color(&quot;#FF&quot;);<br/> &nbsp; &nbsp; this will return the following assoc array if successful:<br/> &nbsp; &nbsp; *[success] = true<br/> &nbsp; &nbsp; *[r] = 255<br/> &nbsp; &nbsp; *[g] = 0<br/> &nbsp; &nbsp; *[b] = 0<br/><br/> &nbsp; &nbsp; or copy and paste the following code:<br/> &nbsp; &nbsp; <br/> &nbsp; &nbsp; $hex = &quot;FFFFFF&quot;; // Color White<br/> &nbsp; &nbsp; $color = convert_color($hex);<br/> &nbsp; &nbsp; var_dump($color);<br/> */ <br/>?&gt;<br/><br/>As you can see, the function &quot;convert_color&quot; accepts a hex # in most acceptable formats and returns an associative array. &nbsp;[success] is set to TRUE if the function succeeds and FALSE if not. &nbsp;The array members [r], [g] and [b] hold the red,green and blue values respectively. &nbsp;If it fails, [error] holds a custom error message.<br/><br/>&quot;strip_chars&quot; is a support function written to remove the unwanted characters from the hex string, and sends the concatenated string back to the calling function. &nbsp;It will accept either a single value or an array of values for the characters to remove. <br/>groobo<br/>11-Feb-2005 03:46 <br/>It&#039;s just a revision to marfastic&#039;s ligten_up script, it simply adds/subtracts mod_color to orig_color.<br/>I use it often to adjust tonals rather than brightness only<br/><br/>&lt;?<br/>function mod_color($orig_color, $mod, $mod_color){<br/> &nbsp; /*<br/> &nbsp; &nbsp; &nbsp; $orig_color - original html color, hex<br/> &nbsp; &nbsp; &nbsp; $mod_color - modifying color, hex<br/> &nbsp; &nbsp; &nbsp; $mod - modifier &#039;+&#039; or &#039;-&#039;<br/> &nbsp; &nbsp; &nbsp; usage: mod_color(&#039;CCCCCC&#039;, &#039;+&#039;, &#039;000033&#039;)<br/> &nbsp; */<br/> &nbsp; // does quick validation<br/> &nbsp; preg_match(&quot;/([0-9]&#124;[A-F]){6}/i&quot;,$orig_color,$orig_arr);<br/> &nbsp; preg_match(&quot;/([0-9]&#124;[A-F]){6}/i&quot;,$mod_color,$mod_arr);<br/> &nbsp; if ($orig_arr[0] &amp;&amp; $mod_arr[0]) {<br/> &nbsp; &nbsp; &nbsp; for ($i=0; $i&lt;6; $i=$i+2) {<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $orig_x = substr($orig_arr[0],$i,2);<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mod_x = substr($mod_arr[0],$i,2);<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($mod == &#039;+&#039;) { $new_x = hexdec($orig_x) + hexdec($mod_x); }<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else { $new_x = hexdec($orig_x) - hexdec($mod_x); }<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($new_x &lt; 0) { $new_x = 0; }<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if ($new_x &gt; 255) { $new_x = 255; };<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $new_x = dechex($new_x);<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $ret .= $new_x;<br/> &nbsp; &nbsp; &nbsp; }<br/> &nbsp; &nbsp; &nbsp; return $ret;<br/> &nbsp; } else { return false; }<br/>}<br/>?&gt; <br/>20-Jan-2005 01:33 <br/>I wondered long time what is the best way to generate RGB-color from HEX-color, and just now i found the simpliest way!<br/><br/>&lt;?php<br/>$hex = &quot;FF00FF&quot;;<br/>$rgb = hexdec($hex); // 16711935<br/>?&gt;<br/><br/>I hope this will save your time! :) <br/>marfastic<br/>10-Dec-2004 11:14 <br/>Here is a function to brighten up any color:<br/><br/>function lighten_up($orig_color, $fraction_denom){<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // return the color between the color and white <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // based on the fraction denom that is passed<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!isset($fraction_denom))<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fraction_denom = 2;<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $highest_val = hexdec(&quot;FF&quot;);<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $r = hexdec(substr($orig_color,0,2));<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $r = ($highest_val-$r)/$fraction_denom + $r;<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $g = hexdec(substr($orig_color,2,2));<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $g = ($highest_val-$g)/$fraction_denom + $g;<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $b = hexdec(substr($orig_color,4,2));<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $b = ($highest_val-$b)/$fraction_denom + $b;<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return dechex($r) . dechex($g) . dechex($b);<br/> &nbsp; &nbsp; &nbsp; } <br/>_meto ALT+q web.de<br/>09-May-2004 12:00 <br/>Damn, this took me some real long time! Maybe it&#039;s helpfull for those who even have no idea of color-Schemes like me ;)<br/><br/>If u want to generate PDF&#039;s for Print Offices u need to set all the colors in CMYK.<br/><br/>Here is a Function that will convert RGB to CMYK<br/><br/>&lt;?<br/>function hex2rgb($hex) {<br/> &nbsp;$color = str_replace(&#039;#&#039;,&#039;&#039;,$hex);<br/> &nbsp;$rgb = array(&#039;r&#039; =&gt; hexdec(substr($color,0,2)),<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;g&#039; =&gt; hexdec(substr($color,2,2)),<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;b&#039; =&gt; hexdec(substr($color,4,2)));<br/> &nbsp;return $rgb;<br/>}<br/><br/>function rgb2cmyk($var1,$g=0,$b=0) {<br/> &nbsp; if(is_array($var1)) {<br/> &nbsp; &nbsp; $r = $var1[&#039;r&#039;];<br/> &nbsp; &nbsp; $g = $var1[&#039;g&#039;];<br/> &nbsp; &nbsp; $b = $var1[&#039;b&#039;];<br/> &nbsp; }<br/> &nbsp; else $r=$var1;<br/> &nbsp; $cyan &nbsp; &nbsp;= 255 - $r;<br/> &nbsp; $magenta = 255 - $g;<br/> &nbsp; $yellow &nbsp;= 255 - $b;<br/> &nbsp; $black &nbsp;= min($cyan, $magenta, $yellow);<br/> &nbsp; $cyan &nbsp; &nbsp;= @(($cyan &nbsp; &nbsp;- $black) / (255 - $black)) * 255;<br/> &nbsp; $magenta = @(($magenta - $black) / (255 - $black)) * 255;<br/> &nbsp; $yellow &nbsp;= @(($yellow &nbsp;- $black) / (255 - $black)) * 255;<br/> &nbsp; return array(&#039;c&#039; =&gt; $cyan / 255,<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;m&#039; =&gt; $magenta / 255,<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;y&#039; =&gt; $yellow / 255,<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;k&#039; =&gt; $black / 255);<br/>}<br/><br/>$color=rgb2cmyk(hex2rgb(&#039;#FF0000&#039;)); <br/>pdf_setcolor($pdf, &quot;both&quot;, &quot;cmyk&quot;, $color[&#039;c&#039;], $color[&#039;m&#039;], $color[&#039;y&#039;], $color[&#039;k&#039;]);<br/>?&gt;<br/><br/>U can call it with parameters (r,g,b) or just with an array(r,g,b) that contains these values.<br/><br/>Hope it works correct. All testing was fine. <br/>gelado at ip3 dot com<br/>29-Aug-2003 07:30 <br/>Another hex to decimal converter, up to the precision of PHP floats.<br/><br/>function longhex($hex)<br/>{<br/> &nbsp; $dec = 0;<br/> &nbsp; $bitval = 1;<br/> &nbsp; for($pos = 1; $pos &lt;= strlen($hex); $pos++) {<br/> &nbsp; &nbsp; &nbsp; $dec += hexdec(substr($hex, -$pos, 1)) * $bitval;<br/> &nbsp; &nbsp; &nbsp; $bitval *= 16;<br/> &nbsp; }<br/> &nbsp; return($dec);<br/>} <br/>henrique at recidive dot com<br/>28-May-2003 06:43 <br/>Function to convert a string with hexadecimal colors to an associative array with RGB values:<br/><br/>&lt;?<br/>function hex2dec($hex) {<br/> &nbsp;$color = str_replace(&#039;#&#039;, &#039;&#039;, $hex);<br/> &nbsp;$ret = array(<br/> &nbsp; &#039;r&#039; =&gt; hexdec(substr($color, 0, 2)),<br/> &nbsp; &#039;g&#039; =&gt; hexdec(substr($color, 2, 2)),<br/> &nbsp; &#039;b&#039; =&gt; hexdec(substr($color, 4, 2))<br/> &nbsp;);<br/> &nbsp;return $ret;<br/>}<br/><br/>/*** Example:<br/>print_r(hex2dec(&#039;3D58BE&#039;));<br/><br/>or<br/><br/>print_r(hex2dec(&#039;#3D58BE&#039;));<br/><br/>will return<br/><br/>Array<br/>(<br/> &nbsp;[r] =&gt; 61<br/> &nbsp;[g] =&gt; 88<br/> &nbsp;[b] =&gt; 190<br/>)<br/>***/<br/>?&gt;<br/><br/>Thanks !<br/><br/>Henrique Recidive <br/>andreas.schmeiler<br/>05-Feb-2003 09:59 <br/>Here&#039;s another hex2bin variant, works pretty well to me.<br/><br/>function hex2bin($hexdata) {<br/> &nbsp; <br/> &nbsp; for ($i=0;$i&lt;strlen($hexdata);$i+=2) {<br/> &nbsp; &nbsp; $bindata.=chr(hexdec(substr($hexdata,$i,2)));<br/> &nbsp; }<br/> &nbsp; <br/> &nbsp; return $bindata;<br/>} <br/>chrism at four dot net<br/>16-Dec-2001 02:02 <br/>hexdec from 4.1.0 onwards does not show <br/>the same size limitation and therefore <br/>works differently with large numbers than previous php versions. <br/><br/>To obtain the same results, use: <br/><br/>(int) hexdec (...) <br/>andychase at yahoo dot com<br/>18-Jan-2001 10:32 <br/>For hex numbers larger than 7FFFFFFF (E.G. PalmOS dates in seconds elapsed since 01/01/1904), I came up with this: <br/><br/>function bighexdec($hexnumber){ <br/><br/> &nbsp; $numlength = strlen($hexnumber); <br/><br/> &nbsp; $decnumber = 0; <br/><br/> &nbsp; for($x = 1; $x &lt;= $numlength; $x++){ <br/><br/> &nbsp; &nbsp; &nbsp; $place = $numlength - $x; <br/> &nbsp; &nbsp; &nbsp; <br/> &nbsp; &nbsp; &nbsp; $operand = hexdec(substr($hexnumber,$place,1)); <br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br/> &nbsp; &nbsp; &nbsp; $exponent = pow(16,$x-1); <br/><br/> &nbsp; &nbsp; &nbsp; $decValue = $operand * $exponent; <br/> &nbsp; <br/> &nbsp; &nbsp; &nbsp; $decnumber += $decValue; <br/><br/> &nbsp; } <br/><br/> &nbsp; return $decnumber; <br/> &nbsp; <br/>} <br/>joe at aigraphics dot com<br/>21-Oct-2000 06:32 <br/>Here is a fast block of code to convert HTML Style HEX colors to RGB: <br/>-------------------- <br/>$r = hexdec(substr($color, 0, 2)); <br/>$g = hexdec(substr($color, 2, 2)); <br/>$b = hexdec(substr($color, 4, 2)); <br/>-------------------- <br/>hope it helps you, enjoy. <br/>
]]>
</description>
</item><item>
<link>http://www.dzhope.com/post//#blogcomment</link>
<title><![CDATA[[评论] php将16进制转换成10进制的函数hexdec]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.dzhope.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>