hexdec() - 十六进制转换为十进制 - php 数学函数
hexdec()
(PHP 4, PHP 5, PHP 7)
十六进制转换为十进制
说明
hexdec(string $hex_string):number返回与$hex_string参数所表示的十六进制数等值的的十进制数。hexdec()将一个十六进制字符串转换为十进制数。
hexdec()会忽略它遇到的任意非十六进制的字符。
参数
$hex_string要转换的十六进制的字符串
返回值
$hex_string十进制的表示
更新日志
版本 | 说明 |
---|---|
4.1.0 | PHP 4.1.0 开始,该函数可以处理integer大数字,这种情况下,它会返回float类型。 |
范例
Example #1hexdec()例子
参见
dechex()
十进制转换为十六进制bindec()
二进制转换为十进制octdec()
八进制转换为十进制base_convert()
在任意进制之间转换数字
Use this function to convert a hexa decimal color code to its RGB equivalent. Unlike many other functions provided here, it will work correctly with hex color short hand notation. Also, if a proper hexa decimal color value is given (6 digits), it uses bit wise operations for faster results. For eg: #FFF and #FFFFFF will produce the same result OUTPUT: hex2RGB("#FF0") -> array( red =>255, green => 255, blue => 0) hex2RGB("#FFFF00) -> Same as above hex2RGB("#FF0", true) -> 255,255,0 hex2RGB("#FF0", true, ":") -> 255:255:0
Here is other function to transform a MAC Address to decimal:
Here's my hexdec function for greater numbers using BC Math
RGB to Hex Hex to RGB Function Output #FFFFFF => Array{ red=>255, green=>255, blue=>255, r=>255, g=>255, b=>255, 0=>255, 1=>255, 2=>255 } #FFCCEE => Array{ red=>255, green=>204, blue=>238, r=>255, g=>204, b=>238, 0=>255, 1=>204, 2=>238 } CC22FF => Array{ red=>204, green=>34, blue=>255, r=>204, g=>34, b=>255, 0=>204, 1=>34, 2=>255 } 0 65 255 => #0041FF 255.150.3 => #FF9603 100,100,250 => #6464FA [EDIT BY danbrown AT php DOT net - Contains multiple bugfixes by (ajim1417 AT gmail DOT com) on 27-JAN-2010: Replaces typo in explode() and updates eregi() calls to preg_match().]
If you want to create or parse signed Hex strings: Also note that ('0x' . $hexstr + 0) is faster than hexdec() (Tested on PHP v5.2.17)
Here My version of converting a hex string to a signed decimal value:
One of my favourite, multi-purpose, bidirectional solution I wrote many years ago: function bgr2rgb($cr) { // bidirectional return (($cr & 0x0000FF) > 16); } Which you might want to use as : function hex2cr($hex) { // strips any leading characters, like # return bgr2rgb(hexdec($hex)); } function cr2hex($cr) { // the usual HTML format, #rrggbb return '#'.str_pad(strtoupper(dechex(bgr2rgb($cr))), 6, '0', STR_PAD_LEFT); } And, if like me you tend to mistype function names, the synonym : function rgb2bgr($val) { return bgr2rgb($val); }
I wondered long time what is the best way to generate RGB-color from HEX-color, and just now i found the simpliest way! I hope this will save your time! :)
I made these functions to pack up to 64 ID's into a mysql unsigned bigint. ID's cannot repeat, must be Have fun ;D
The issue I've seen with the existing hex to dec conversion routines is the lack of error-trapping. I stick to the theory that one should try to cover ALL the bases when writing a generalized routine such as this one. I have a varied background that covers a wide variety of design/development languages, on the web as well as desktop apps. As such I've seen multiple formats for writing hex colors. For example, the color red COULD be written as follows: #ff0000 &Hff0000 #ff &Hff 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. As you can see, the function "convert_color" accepts a hex # in most acceptable formats and returns an associative array. [success] is set to TRUE if the function succeeds and FALSE if not. The array members [r], [g] and [b] hold the red,green and blue values respectively. If it fails, [error] holds a custom error message. "strip_chars" is a support function written to remove the unwanted characters from the hex string, and sends the concatenated string back to the calling function. It will accept either a single value or an array of values for the characters to remove.
// Função GET Cor Hexadecima e Retorna em RGB function hexrgb($hexstr, $rgb) { $int = hexdec($hexstr); switch($rgb) { case "r": return 0xFF & $int >> 0x10; break; case "g": return 0xFF & ($int >> 0x8); break; case "b": return 0xFF & $int; break; default: return array( "r" => 0xFF & $int >> 0x10, "g" => 0xFF & ($int >> 0x8), "b" => 0xFF & $int ); break; } }// END GET Cor Hex => RGB //Uso echo hexrgb("1a2b3c", r); // 26 echo hexrgb("1a2b3c", g); // 43 echo hexrgb("1a2b3c", b); // 60 //ou var_dump(hexrgb("1a2b3c", rgb)); //array(3) { ["r"]=> int(26) ["g"]=> int(43) ["b"]=> int(60) }
After esnhexdec from "rledger at gmail dot com", the esndechex:
hexdec from 4.1.0 onwards does not show the same size limitation and therefore works differently with large numbers than previous php versions. To obtain the same results, use: (int) hexdec (...)
hexdec doesn't accept numbers following the period. What if you have a number like c20.db18?
Help a hex-stricken string get back to normal:
function hex2rgb($hex) { if ($hex[0]=='#') $hex = substr($hex,1); if (strlen($hex)==3){ $hex = $hex[0].$hex[0].$hex[1].$hex[1].$hex[2].$hex[2]; } $int = hexdec($hex); return array("red" => 0xFF & ($int >> 0x10), "green" => 0xFF & ($int >> 0x8), "blue" => 0xFF & $int); }
This tiny function will return foreground colors (either black or white) in contrast to the color you provide: This function will return the opposite (negative):
Here is my version of hex2rgb for web colors to 24bit colors.
From color to color to ...... to color with fade effect. Good for dynamic bar chart. Repley.
A handy little function to convert HEX colour codes to "web safe" colours... Example: color_mkwebsafe('0e5c94'); Produces: 006699 Hope this helps someone out... Happy coding. :-)
I made this for a little phpbb mod. It was used to take the hex value from the database and make a color 20 (in decimal) less, resulting a darker color. EXAMPLE: #336699 to #1f5285
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: Handles 2, 3, and 6 character colour codes with leading # or &H.
This replaces my previous class. I've added a few more input checks in the rgb2hex function. Also it returned incorrect hex values for 1-digit values. color::rgb2hex(array(0,0,0)) would output 000 not 00000.
It's just a revision to marfastic's ligten_up script, it simply adds/subtracts mod_color to orig_color. I use it often to adjust tonals rather than brightness only
Here's another hex2bin variant, works pretty well to me. function hex2bin($hexdata) { for ($i=0;$i 0, "1" => 1, "2" => 2, "3" => 3, "4" => 4, "5" => 5, "6" => 6, "7" => 7, "8" => 8, "9" => 9, "A" => 10, "B" => 11, "C" => 12, "D" => 13, "E" => 14, "F" => 15 ); $dec = 0; for ($i = strlen($hex) - 1, $e = 1; $i >= 0; $i--, $e = bcmul($e, 16)) { $factor = $hexdec[$hex[$i]]; $dec = bcadd($dec, bcmul($factor, $e)); } return $dec; }
In reply to Amit Yadav's post (hex to binary conversion): function binfromdec($num) { $primary = "bit"; for ($i=1; $i 32766) return ("Too Large!"); if ($num & 16384) $bit15 = 1; if ($num & 8192) $bit14 = 1; if ($num & 4096) $bit13 = 1; if ($num & 2048) $bit12 = 1; if ($num & 1024) $bit11 = 1; if ($num & 512) $bit10 = 1; if ($num & 256) $bit9 = 1; if ($num & 128) $bit8 = 1; if ($num & 64) $bit7 = 1; if ($num & 32) $bit6 = 1; if ($num & 16) $bit5 = 1; if ($num & 8) $bit4 = 1; if ($num & 4) $bit3 = 1; if ($num & 2) $bit2 = 1; if ($num & 1) $bit1 = 1; return ("" . $bit15 . $bit14 . $bit13 . $bit12 . $bit11 . $bit10 . $bit9 . $bit8 . $bit7 . $bit6 . $bit5 . $bit4 . $bit3 . $bit2 . $bit1); }
Here's a short example to convert strings between hex and chars:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)