{"id":57,"date":"2019-08-30T18:32:06","date_gmt":"2019-08-30T18:32:06","guid":{"rendered":"https:\/\/tutorialio.com\/?p=57"},"modified":"2019-08-30T18:32:06","modified_gmt":"2019-08-30T18:32:06","slug":"binary-hexadecimal-octal-functions-in-php","status":"publish","type":"post","link":"https:\/\/hellonitish.com\/blog\/binary-hexadecimal-octal-functions-in-php\/","title":{"rendered":"Binary, Hexadecimal, Octal Functions in PHP"},"content":{"rendered":"<table class=\"quick-links\">\n<caption>Random Number Functions Functions in PHP<\/caption>\n<\/caption>\n<tbody>\n<tr>\n<th>Function Name<\/th>\n<th>Function Description<\/th>\n<\/tr>\n<tr>\n<td><a href=\"#baseconvert\">base_convert()<\/a><\/td>\n<td>Convert a number between arbitrary bases<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#bindec\">bindec()<\/a><\/td>\n<td>Binary to decimal<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#decbin\">decbin()<\/a><\/td>\n<td>Decimal to binary<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#dechex\">dechex()<\/a><\/td>\n<td>Decimal to hexadecimal<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#decoct\">decoct()<\/a><\/td>\n<td>Decimal to octal<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#hexdec\">hexdec()<\/a><\/td>\n<td>Hexadecimal to decimal<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#octdec\">octdec()<\/a><\/td>\n<td>Octal to decimal<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"base_convert\">PHP <code>base_convert()<\/code> Function<\/h2>\n<h3>PHP <code>base_convert()<\/code> Usage<\/h3>\n<p>The PHP <code><code>base_convert()<\/code><\/code> function will convert a number between arbitrary bases.<\/p>\n<h3>PHP <code>base_convert()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> base_convert ( string $number , int $frombase , int $tobase ) : string<\/code><\/pre>\n<h3>PHP <code>base_convert()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>number<\/code> \u2014 The number to convert. Any invalid characters in number are silently ignored. As of PHP 7.4.0 supplying any invalid characters is deprecated. <\/p>\n<\/li>\n<li>\n<p><code>frombase<\/code> \u2014 The base number is in <\/p>\n<\/li>\n<li>\n<p><code>tobase<\/code> \u2014 The base to convert number to <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>base_convert()<\/code> Return Value<\/h3>\n<p>The PHP <code>base_convert()<\/code> function returns <code class=\"parameter\">number<\/code> converted to base <code class=\"parameter\">tobase<\/code><\/p>\n<h3>PHP <code>base_convert()<\/code> Working Examples<\/h3>\n<h4>1. base_convert() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$hexadecimal = 'a37334';\necho base_convert($hexadecimal, 16, 2);\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">101000110111001100110100<\/code><\/pre>\n<h3>Changelog for PHP base_convert()<\/code> Function<\/h3>\n<p><strong>7.4.0 \u2014 <\/strong> Passing invalid characters will now generate a deprecation notice. The result will still be computed as if the invalid characters did not exist. <\/p>\n<h3>Important Points about PHP <code>base_convert()<\/code> Function<\/h3>\n<ol>\n<li>\n<p> <code>base_convert()<\/code> may lose precision on large numbers due to properties related to the internal &#8220;double&#8221; or &#8220;float&#8221; type used. Please see the Floating point numbers section in the manual for more specific information and limitations. <\/p>\n<\/li>\n<\/ol>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: PHPCoder<\/p>\n<pre><code>Convert an arbitrarily large number from any base to any base.\nstring convBase(string $numberInput, string $fromBaseInput, string $toBaseInput)\n$numberInput number to convert as a string\n$fromBaseInput base of the number to convert as a string\n$toBaseInput base the number should be converted to as a string\nexamples for $fromBaseInput and $toBaseInput\n'0123456789ABCDEF' for Hexadecimal (Base16)\n'0123456789' for Decimal (Base10)\n'01234567' for Octal (Base8)\n'01' for Binary (Base2) \nYou can really put in whatever you want and the first character is the 0.\nExamples:\n<br>&lt;?php \nconvBase('123', '0123456789', '01234567'); \n\/\/Convert '123' from decimal (base10) to octal (base8).\n\/\/result: 173\nconvBase('70B1D707EAC2EDF4C6389F440C7294B51FFF57BB', '0123456789ABCDEF', '01');\n\/\/Convert '70B1D707EAC2EDF4C6389F440C7294B51FFF57BB' from hexadecimal (base16) to binary (base2).\n\/\/result: \n\/\/111000010110001110101110000011111101010110000101110\n\/\/110111110100110001100011100010011111010001000000110\n\/\/001110010100101001011010100011111111111110101011110\n\/\/111011\nconvBase('1324523453243154324542341524315432113200203012', '012345', '0123456789ABCDEF');\n\/\/Convert '1324523453243154324542341524315432113200203012' from senary (base6) to hexadecimal (base16).\n\/\/result: 1F9881BAD10454A8C23A838EF00F50\nconvBase('355927353784509896715106760','0123456789','Christopher');\n\/\/Convert '355927353784509896715106760' from decimal (base10) to undecimal (base11) using \"Christopher\" as the numbers.\n\/\/result: iihtspiphoeCrCeshhorsrrtrh\nconvBase('1C238Ab97132aAC84B72','0123456789aAbBcCdD', '~!@#$%^&amp;*()');\n\/\/Convert'1C238Ab97132aAC84B72' from octodecimal (base18) using '0123456789aAbBcCdD' as the numbers to undecimal (base11) using '~!@#$%^&amp;*()' as the numbers.\n\/\/result: !%~!!*&amp;!~^!!&amp;(&amp;!~^@#@@@&amp;\nfunction convBase($numberInput, $fromBaseInput, $toBaseInput)\n{\n    if ($fromBaseInput==$toBaseInput) return $numberInput;\n    $fromBase = str_split($fromBaseInput,1);\n    $toBase = str_split($toBaseInput,1);\n    $number = str_split($numberInput,1);\n    $fromLen=strlen($fromBaseInput);\n    $toLen=strlen($toBaseInput);\n    $numberLen=strlen($numberInput);\n    $retval='';\n    if ($toBaseInput == '0123456789')\n    {\n        $retval=0;\n        for ($i = 1;$i &lt;= $numberLen; $i++)\n            $retval = bcadd($retval, bcmul(array_search($number[$i-1], $fromBase),bcpow($fromLen,$numberLen-$i)));\n        return $retval;\n    }\n    if ($fromBaseInput != '0123456789')\n        $base10=convBase($numberInput, $fromBaseInput, '0123456789');\n    else\n        $base10 = $numberInput;\n    if ($base10&lt;strlen($toBaseInput))\n        return $toBase[$base10];\n    while($base10 != '0')\n    {\n        $retval = $toBase[bcmod($base10,$toLen)].$retval;\n        $base10 = bcdiv($base10,$toLen,0);\n    }\n    return $retval;\n}\n?&gt;<br><\/pre>\n<p><\/code><\/p>\n<p>Contributed By: JR<\/p>\n<pre><code>Short arabic2roman conveter: \n \n<br>&lt;?php \nfunction rome($N){ \n        $c='IVXLCDM'; \n        for($a=5,$b=$s='';$N;$b++,$a^=7) \n                for($o=$N%$a,$N=$N\/$a^0;$o--;$s=$c[$o&gt;2?$b+$N-($N&amp;=-2)+$o=1:$b].$s); \n        return $s; \n} \n?&gt;<br> \n \nAnd it works :)<\/pre>\n<p><\/code><\/p>\n<p>Contributed By: Clifford dot ct<\/p>\n<pre><code>If you use base_convert to convert a large (eg. 80-bit) hexadecimal to base-36, you might observe: \n \nABCDEF00001234567890 (hexadecimal) =&gt; 3O47RE02JZSW0KS8 (base-36) =&gt; ABCDEF00001240000000 (hexadecimal) \n \nThis is normal and is due to the loss of precision on large numbers. \n \nI have written a string-based function using the built-in BC Math Extension which will overcome this and similar problems. \n \n<br>&lt;?php \n \nfunction str_baseconvert($str, $frombase=10, $tobase=36) { \n    $str = trim($str); \n    if (intval($frombase) != 10) { \n        $len = strlen($str); \n        $q = 0; \n        for ($i=0; $i&lt;$len; $i++) { \n            $r = base_convert($str[$i], $frombase, 10); \n            $q = bcadd(bcmul($q, $frombase), $r); \n        } \n    } \n    else $q = $str; \n \n    if (intval($tobase) != 10) { \n        $s = ''; \n        while (bccomp($q, '0', 0) &gt; 0) { \n            $r = intval(bcmod($q, $tobase)); \n            $s = base_convert($r, 10, $tobase) . $s; \n            $q = bcdiv($q, $tobase, 0); \n        } \n    } \n    else $s = $q; \n \n    return $s; \n} \n \n?&gt;<br> \n \nTypical use-cases: \n1.  Convert a large arbitrary precision number to base-36. \n2.  Convert a 32-char hexadecimal UUID (128-bit) to a 25-char base-36 unique key, and vice versa. \n \nExamples: \n \n<br>&lt;?php \n$b16 = 'ABCDEF00001234567890'; \n$b36 = str_baseconvert($b16, 16, 36); \necho (\"$b16 (hexadecimal) = $b36 (base-36) \\\\n\"); \n \n$uuid = 'ABCDEF01234567890123456789ABCDEF'; \n$ukey = str_baseconvert($uuid, 16, 36); \necho (\"$uuid (hexadecimal) = $ukey (base-36) \\\\n\"); \n?&gt;<br> \n \nABCDEF00001234567890 (hexadecimal) = 3o47re02jzqisvio (base-36) \nABCDEF01234567890123456789ABCDEF (hexadecimal) = a65xa07491kf5zyfpvbo76g33 (base-36)<\/pre>\n<p><\/code><\/p>\n<h2 id=\"bindec\">PHP <code>bindec()<\/code> Function<\/h2>\n<h3>PHP <code>bindec()<\/code> Usage<\/h3>\n<p>The PHP <code><code>bindec()<\/code><\/code> function will give you the decimal equivalent of the binary number represented by the <code class=\"parameter\">binary_string<\/code> argument.<\/p>\n<h3>PHP <code>bindec()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> bindec ( string $binary_string ) : number<\/code><\/pre>\n<h3>PHP <code>bindec()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>binary_string<\/code> \u2014 The binary string to convert. Any invalid characters in binary_string are silently ignored. As of PHP 7.4.0 supplying any invalid characters is deprecated. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>bindec()<\/code> Return Value<\/h3>\n<p>The PHP <code>bindec()<\/code> function returns the decimal value of <code class=\"parameter\">binary_string<\/code><\/p>\n<h3>PHP <code>bindec()<\/code> Working Examples<\/h3>\n<h4>1. bindec() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho bindec('110011') . \"\\n\";\necho bindec('000110011') . \"\\n\";\necho bindec('111');\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">51\n51\n7<\/code><\/pre>\n<h4>2. bindec() interprets input as unsigned integers<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n\/*\n * The lesson from this example is in the output\n * rather than the PHP code itself.\n *\/\n$magnitude_lower = pow(2, (PHP_INT_SIZE * 8) - 2);\np($magnitude_lower - 1);\np($magnitude_lower, 'See the rollover?  Watch it next time around...');\np(PHP_INT_MAX, 'PHP_INT_MAX');\np(~PHP_INT_MAX, 'interpreted to be one more than PHP_INT_MAX');\nif (PHP_INT_SIZE == 4) {\n    $note = 'interpreted to be the largest unsigned integer';\n} else {\n    $note = 'interpreted to be the largest unsigned integer\n              (18446744073709551615) but skewed by float precision';\n}\np(-1, $note);\nfunction p($input, $note = '') {\n    echo \"input:        $input\\n\";\n    $format = '%0' . (PHP_INT_SIZE * 8) . 'b';\n    $bin = sprintf($format, $input);\n    echo \"binary:       $bin\\n\";\n    ini_set('precision', 20);  \/\/ For readability on 64 bit boxes.\n    $dec = bindec($bin);\n    echo 'bindec():     ' . $dec . \"\\n\";\n    if ($note) {\n        echo \"NOTE:         $note\\n\";\n    }\n    echo \"\\n\";\n}\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">input: 1073741823\nbinary: 00111111111111111111111111111111\nbindec(): 1073741823\ninput: 1073741824\nbinary: 01000000000000000000000000000000\nbindec(): 1073741824\nNOTE: See the rollover? Watch it next time around...\ninput: 2147483647\nbinary: 01111111111111111111111111111111\nbindec(): 2147483647\nNOTE: PHP_INT_MAX\ninput: -2147483648\nbinary: 10000000000000000000000000000000\nbindec(): 2147483648\nNOTE: interpreted to be one more than PHP_INT_MAX\ninput: -1\nbinary: 11111111111111111111111111111111\nbindec(): 4294967295\nNOTE: interpreted to be the largest unsigned integer<\/code><\/pre>\n<h3>Changelog for PHP bindec()<\/code> Function<\/h3>\n<p><strong>7.4.0 \u2014 <\/strong> Passing invalid characters will now generate a deprecation notice. The result will still be computed as if the invalid characters did not exist. <\/p>\n<h3>Important Points about PHP <code>bindec()<\/code> Function<\/h3>\n<ol>\n<li>\n<p> The parameter must be a string. Using other data types will produce unexpected results. <\/p>\n<\/li>\n<li>\n<p> The function can convert numbers that are too large to fit into the platforms integer type, larger values are returned as float in that case. <\/p>\n<\/li>\n<\/ol>\n<h2 id=\"decbin\">PHP <code>decbin()<\/code> Function<\/h2>\n<h3>PHP <code>decbin()<\/code> Usage<\/h3>\n<p>The PHP <code><code>decbin()<\/code><\/code> function will give you a string containing a binary representation of the given <code class=\"parameter\">number<\/code> argument.<\/p>\n<h3>PHP <code>decbin()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> decbin ( int $number ) : string<\/code><\/pre>\n<h3>PHP <code>decbin()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>number<\/code> \u2014 Decimal value to convert <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>decbin()<\/code> Return Value<\/h3>\n<p>The PHP <code>decbin()<\/code> function returns binary string representation of <code class=\"parameter\">number<\/code><\/p>\n<h3>PHP <code>decbin()<\/code> Working Examples<\/h3>\n<h4>1. decbin() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho decbin(12) . \"\\n\";\necho decbin(26);\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">1100\n11010<\/code><\/pre>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: MarcelG<\/p>\n<pre><code>To add leading zeros I prefer the following:\n<br>&lt;?php\n\/\/ Add leading zeros\n$bin = sprintf( \"%08d\", decbin( 26 )); \/\/ \"00011010\"\n?&gt;<br><\/pre>\n<p><\/code><\/p>\n<h2 id=\"dechex\">PHP <code>dechex()<\/code> Function<\/h2>\n<h3>PHP <code>dechex()<\/code> Usage<\/h3>\n<p>The PHP <code><code>dechex()<\/code><\/code> function will give you a string containing a hexadecimal representation of the given unsigned <code class=\"parameter\">number<\/code> argument.<\/p>\n<h3>PHP <code>dechex()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> dechex ( int $number ) : string<\/code><\/pre>\n<h3>PHP <code>dechex()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>number<\/code> \u2014 The decimal value to convert. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>dechex()<\/code> Return Value<\/h3>\n<p>The PHP <code>dechex()<\/code> function returns hexadecimal string representation of <code class=\"parameter\">number<\/code>.<\/p>\n<h3>PHP <code>dechex()<\/code> Working Examples<\/h3>\n<h4>1. dechex() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho dechex(10) . \"\\n\";\necho dechex(47);\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">a\n2f<\/code><\/pre>\n<h4>2. dechex() example with large integers<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n\/\/ The output below assumes a 32-bit platform.\n\/\/ Note that the output is the same for all values.\necho dechex(-1).\"\\n\";\necho dechex(PHP_INT_MAX * 2 + 1).\"\\n\";\necho dechex(pow(2, 32) - 1).\"\\n\";\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">ffffffff\nffffffff\nffffffff<\/code><\/pre>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: brent<\/p>\n<pre><code>Be very careful calling dechex on a number if it's stored in a string.\nFor instance:\nThe max number it can handle is 4294967295 which in hex is FFFFFFFF, as it says in the documentation.\ndechex(4294967295) =&gt; FFFFFFFF \/\/CORRECT\nBUT, if you call it on a string of a number, it casts to int, and automatically gives you the largest int it can handle.\ndechex('4294967295') =&gt; 7FFFFFFF \/\/WRONG!\nso you'll need to cast to a float:\ndechex((float) '4294967295') =&gt; FFFFFFFF \/\/CORRECT\nThis took me FOREVER to figure out, so hopefully I just saved someone some time.<\/pre>\n<p><\/code><\/p>\n<p>Contributed By: joost<\/p>\n<pre><code>Here are two functions that will convert large dec numbers to hex and vice versa. And I really mean LARGE, much larger than any function posted earlier.\n&lt;pre&gt;\n\/\/ Input: A decimal number as a String.\n\/\/ Output: The equivalent hexadecimal number as a String.\nfunction dec2hex($number)\n{\n    $hexvalues = array('0','1','2','3','4','5','6','7',\n               '8','9','A','B','C','D','E','F');\n    $hexval = '';\n     while($number != '0')\n     {\n        $hexval = $hexvalues[bcmod($number,'16')].$hexval;\n        $number = bcdiv($number,'16',0);\n    }\n    return $hexval;\n}\n\/\/ Input: A hexadecimal number as a String.\n\/\/ Output: The equivalent decimal number as a String.\nfunction hex2dec($number)\n{\n    $decvalues = array('0' =&gt; '0', '1' =&gt; '1', '2' =&gt; '2',\n               '3' =&gt; '3', '4' =&gt; '4', '5' =&gt; '5',\n               '6' =&gt; '6', '7' =&gt; '7', '8' =&gt; '8',\n               '9' =&gt; '9', 'A' =&gt; '10', 'B' =&gt; '11',\n               'C' =&gt; '12', 'D' =&gt; '13', 'E' =&gt; '14',\n               'F' =&gt; '15');\n    $decval = '0';\n    $number = strrev($number);\n    for($i = 0; $i &lt; strlen($number); $i++)\n    {\n        $decval = bcadd(bcmul(bcpow('16',$i,0),$decvalues[$number{$i}]), $decval);\n    }\n    return $decval;\n}\n&lt;\/pre&gt;<\/pre>\n<p><\/code><\/p>\n<h2 id=\"decoct\">PHP <code>decoct()<\/code> Function<\/h2>\n<h3>PHP <code>decoct()<\/code> Usage<\/h3>\n<p>The PHP <code><code>decoct()<\/code><\/code> function will give you a string containing an octal representation of the given <code class=\"parameter\">number<\/code> argument. The largest number that can be converted depends on the platform in use. For 32-bit platforms this is usually <strong>4294967295<\/strong> in decimal resulting in <strong>37777777777<\/strong>. For 64-bit platforms this is usually <strong>9223372036854775807<\/strong> in decimal resulting in <strong>777777777777777777777<\/strong>.<\/p>\n<h3>PHP <code>decoct()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> decoct ( int $number ) : string<\/code><\/pre>\n<h3>PHP <code>decoct()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>number<\/code> \u2014 Decimal value to convert <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>decoct()<\/code> Return Value<\/h3>\n<p>The PHP <code>decoct()<\/code> function returns octal string representation of <code class=\"parameter\">number<\/code><\/p>\n<h3>PHP <code>decoct()<\/code> Working Examples<\/h3>\n<h4>1. decoct() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho decoct(15) . \"\\n\";\necho decoct(264);\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">17\n410<\/code><\/pre>\n<h2 id=\"hexdec\">PHP <code>hexdec()<\/code> Function<\/h2>\n<h3>PHP <code>hexdec()<\/code> Usage<\/h3>\n<p>The PHP <code><code>hexdec()<\/code><\/code> function will give you the decimal equivalent of the hexadecimal number represented by the <code class=\"parameter\">hex_string<\/code> argument. <code>hexdec()<\/code> converts a hexadecimal string to a decimal number.<\/p>\n<h3>PHP <code>hexdec()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> hexdec ( string $hex_string ) : number<\/code><\/pre>\n<h3>PHP <code>hexdec()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>hex_string<\/code> \u2014 The hexadecimal string to convert <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>hexdec()<\/code> Return Value<\/h3>\n<p>The PHP <code>hexdec()<\/code> function returns the decimal representation of <code class=\"parameter\">hex_string<\/code><\/p>\n<h3>PHP <code>hexdec()<\/code> Working Examples<\/h3>\n<h4>1. hexdec() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\nvar_dump(hexdec(\"See\"));\nvar_dump(hexdec(\"ee\"));\n\/\/ both print \"int(238)\"\nvar_dump(hexdec(\"that\")); \/\/ print \"int(10)\"\nvar_dump(hexdec(\"a0\")); \/\/ print \"int(160)\"\n?&gt;<\/code><\/pre>\n<h3>Changelog for PHP hexdec()<\/code> Function<\/h3>\n<p><strong>7.4.0 \u2014 <\/strong> Passing invalid characters will now generate a deprecation notice. The result will still be computed as if the invalid characters did not exist. <\/p>\n<h3>Important Points about PHP <code>hexdec()<\/code> Function<\/h3>\n<ol>\n<li>\n<p> The function can convert numbers that are too large to fit into the platforms integer type, larger values are returned as float in that case. <\/p>\n<\/li>\n<\/ol>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: hafees<\/p>\n<pre><code>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.\nAlso, if a proper hexa decimal color value is given (6 digits), it uses bit wise operations for faster results.\nFor eg: #FFF and #FFFFFF will produce the same result\n<br>&lt;?php\n\/**\n * Convert a hexa decimal color code to its RGB equivalent\n *\n * @param string $hexStr (hexadecimal color value)\n * @param boolean $returnAsString (if set true, returns the value separated by the separator character. Otherwise returns associative array)\n * @param string $seperator (to separate RGB values. Applicable only if second parameter is true.)\n * @return array or string (depending on second parameter. Returns False if invalid hex color value)\n *\/                                                                                                 \nfunction hex2RGB($hexStr, $returnAsString = false, $seperator = ',') {\n    $hexStr = preg_replace(\"\/[^0-9A-Fa-f]\/\", '', $hexStr); \/\/ Gets a proper hex string\n    $rgbArray = array();\n    if (strlen($hexStr) == 6) { \/\/If a proper hex code, convert using bitwise operation. No overhead... faster\n        $colorVal = hexdec($hexStr);\n        $rgbArray['red'] = 0xFF &amp; ($colorVal &gt;&gt; 0x10);\n        $rgbArray['green'] = 0xFF &amp; ($colorVal &gt;&gt; 0x8);\n        $rgbArray['blue'] = 0xFF &amp; $colorVal;\n    } elseif (strlen($hexStr) == 3) { \/\/if shorthand notation, need some string manipulations\n        $rgbArray['red'] = hexdec(str_repeat(substr($hexStr, 0, 1), 2));\n        $rgbArray['green'] = hexdec(str_repeat(substr($hexStr, 1, 1), 2));\n        $rgbArray['blue'] = hexdec(str_repeat(substr($hexStr, 2, 1), 2));\n    } else {\n        return false; \/\/Invalid hex color code\n    }\n    return $returnAsString ? implode($seperator, $rgbArray) : $rgbArray; \/\/ returns the rgb string or the associative array\n} ?&gt;<br>\nOUTPUT:\nhex2RGB(\"#FF0\") -&gt; array( red =&gt;255, green =&gt; 255, blue =&gt; 0)\nhex2RGB(\"#FFFF00) -&gt; Same as above\nhex2RGB(\"#FF0\", true) -&gt; 255,255,0\nhex2RGB(\"#FF0\", true, \":\") -&gt; 255:255:0<\/pre>\n<p><\/code><\/p>\n<h2 id=\"octdec\">PHP <code>octdec()<\/code> Function<\/h2>\n<h3>PHP <code>octdec()<\/code> Usage<\/h3>\n<p>The PHP <code><code>octdec()<\/code><\/code> function will give you the decimal equivalent of the octal number represented by the <code class=\"parameter\">octal_string<\/code> argument.<\/p>\n<h3>PHP <code>octdec()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> octdec ( string $octal_string ) : number<\/code><\/pre>\n<h3>PHP <code>octdec()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>octal_string<\/code> \u2014 The octal string to convert. Any invalid characters in octal_string are silently ignored. As of PHP 7.4.0 supplying any invalid characters is deprecated. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>octdec()<\/code> Return Value<\/h3>\n<p>The PHP <code>octdec()<\/code> function returns the decimal representation of <code class=\"parameter\">octal_string<\/code><\/p>\n<h3>PHP <code>octdec()<\/code> Working Examples<\/h3>\n<h4>1. octdec() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho octdec('77') . \"\\n\";\necho octdec(decoct(45));\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">63\n45<\/code><\/pre>\n<h3>Changelog for PHP octdec()<\/code> Function<\/h3>\n<p><strong>7.4.0 \u2014 <\/strong> Passing invalid characters will now generate a deprecation notice. The result will still be computed as if the invalid characters did not exist. <\/p>\n<h3>Important Points about PHP <code>octdec()<\/code> Function<\/h3>\n<ol>\n<li>\n<p> The function can convert numbers that are too large to fit into the platforms integer type, larger values are returned as float in that case. <\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Random Number Functions Functions in PHP Function Name Function Description base_convert() Convert a number between arbitrary bases bindec() Binary to decimal decbin() Decimal to binary dechex() Decimal to hexadecimal decoct() Decimal to octal hexdec() Hexadecimal to decimal octdec() Octal to decimal PHP base_convert() Function PHP base_convert() Usage The PHP base_convert() function will convert a number [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[8,5],"class_list":["post-57","post","type-post","status-publish","format-standard","hentry","category-reference","tag-math","tag-php"],"_links":{"self":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/comments?post=57"}],"version-history":[{"count":0,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/57\/revisions"}],"wp:attachment":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/media?parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/categories?post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/tags?post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}