十六进制转二进制

@param bool $hex

@return string
435415
private function hex2bin($hex = false)
    {
        $ret = $hex !== false && preg_match('/^[0-9a-fA-F]+$/i', $hex) ? pack("H*", $hex) : false;
        return $ret;
    }
1237345
public function binConv($hex)
    {
        $this->preOpMethodParamsCheck(array($hex));

        switch ($this->Test($hex)) {
            case 'dec':
                $hex = $this->encodeHex($hex);
                break;
            case 'hex':
                $hex = $this->addHexPrefix($this->prepAndClean($hex));
                break;
            default:
                throw new \Exception('Unknown data type passed to the binConv() function.  Value received was "' . var_export($hex, true) . '".');
        }

        return strrev($this->encodeValue($hex, '256'));
    }
1344341
protected function char2hex($a)
    {
        return
            $a >= 48 && $a <= 57 ? $a - 48 : // 0-9
                ($a >= 65 && $a <= 70 ? $a - 55 : // A-F
                    ($a >= 97 && $a <= 102 ? $a - 87 : -1)); // a-f
    }
891557
private static function _get_hue( $hex ) {
		$red   = hexdec( substr( $hex, 0, 2 ) );
		$green = hexdec( substr( $hex, 2, 2 ) );
		$blue  = hexdec( substr( $hex, 4, 2 ) );

		if ( $red === $green && $red === $blue ) {
			return 0;
		}

		$hue = static::_get_hue_based_max_rgb( $red, $green, $blue );

		if ( 0 > $hue ) {
			$hue += 360;
		}

		return $hue;
	}
615091
protected function hexToRgb($hex)
    {
        if (! isset($this->colorCache[$hex])) {
            $this->colorCache[$hex] = [
                'r' => hexdec(substr($hex, 0, 2)),
                'g' => hexdec(substr($hex, 2, 2)),
                'b' => hexdec(substr($hex, 4, 2))
            ];
        }

        return $this->colorCache[$hex];
    }
1332922
public static function bigHexToBigDec(string $hex): string
    {
		$dec = '0';
		$len = \strlen($hex);
		for ($i = 1; $i <= $len; $i++) {
			$dec = bcadd($dec, bcmul(\strval(hexdec($hex[$i - 1])), bcpow('16', \strval($len - $i))));
		}

		return $dec;
	}
1258352
public function _xml_entities ($s, $hex = true) {
		// if the string is empty
		if (empty($s)) {
			// just return it
			return $s;
		}
		$s = (string) $s;
		
		// create the return string
		$r = '';
		// get the length
		$l = strlen($s);
		
		// iterate the string
		for ($i = 0; $i < $l; $i++) {
			// get the value of the character
			$o = $this->_unicode_ord($s, $i);
			
			// valid characters
			$v = (
				// \t \n <vertical tab> <form feed> \r
				($o >= 9 && $o <= 13) || 
				// <space> !
				($o == 32) || ($o == 33) || 
				// # $ %
				($o >= 35 && $o <= 37) || 
				// ( ) * + , - . /
				($o >= 40 && $o <= 47) || 
				// numbers
				($o >= 48 && $o <= 57) ||
				// : ;
				($o == 58) || ($o == 59) ||
				// = ?
				($o == 61) || ($o == 63) ||
				// @
				($o == 64) ||
				// uppercase
				($o >= 65 && $o <= 90) ||
				// [ \ ] ^ _ `
				($o >= 91 && $o <= 96) || 
				// lowercase
				($o >= 97 && $o <= 122) || 
				// { | } ~
				($o >= 123 && $o <= 126)
			);
			
			// if it's valid, just keep it
			if ($v) {
				$r .= $s[$i];
			
			// &
			} elseif ($o == 38) {
				$r .= '&amp;';
			
			// <
			} elseif ($o == 60) {
				$r .= '&lt;';
			
			// >
			} elseif ($o == 62) {
				$r .= '&gt;';
			
			// '
			} elseif ($o == 39) {
				$r .= '&apos;';
			
			// "
			} elseif ($o == 34) {
				$r .= '&quot;';
			
			// unknown, add it as a reference
			} elseif ($o > 0) {
				if ($hex) {
					$r .= '&#x'.strtoupper(dechex($o)).';';
					
				} else {
					$r .= '&#'.$o.';';
				}
			}
		}
		
		return $r;
	}
5794
function _hexToDecColor($hex)
    {
        $length = strlen($hex);
        $color['red'] = hexdec(substr($hex, $length - 6, 2));
        $color['green'] = hexdec(substr($hex, $length - 4, 2));
        $color['blue'] = hexdec(substr($hex, $length - 2, 2));
        return $color;
    }
957633
public static function binConv($hex)
    {
        $rem    = '';
        $dv     = '';
        $byte   = '';
        $digits = array();

        for ($x = 0; $x < 256; $x++) {
            $digits[$x] = chr($x);
        }

        if (substr(strtolower($hex), 0, 2) != '0x') {
            $hex = '0x'.strtolower($hex);
        }

        while (Math::cmp($hex, 0) > 0) {
            $dv   = Math::div($hex, 256);
            $rem  = Math::mod($hex, 256);
            $hex  = $dv;
            $byte = $byte.$digits[$rem];
        }

        return strrev($byte);
    }
150975
public static function fromHex($hex)
  {
    if(strlen($hex) != 2)
    {
      throw new TeamSpeak3_Helper_Exception("given parameter '" . $hex . "' is not a valid hexadecimal number");
    }

    return new self(chr(hexdec($hex)));
  }
188048
public function toString(bool $compact = false): string
    {
        if ($compact) {
            return \bin2hex($this->value);
        }
        
        return \vsprintf('%s%s-%s-%s-%s-%s%s%s', \str_split(\bin2hex($this->value), 4));
    }
399159
public static function HexToAscii($hex)
   {
      $ascii = '';
   
      if (strlen($hex) % 2 == 1)
         $hex = '0'.$hex;
   
      for($i = 0; $i < strlen($hex); $i += 2)
         $ascii .= chr(base_convert(substr($hex, $i, 2), 16, 10));
   
      return $ascii;
   }
481645
public function toPlain($hex = null)
    {
        if (empty($this->payload) && empty($hex))
            return "";

        $plain = "";
        $payload = $hex ?: $this->payload;
        for ($c = 0, $cnt = strlen($payload); $c < $cnt; $c += 2) {
            $hex = substr($payload, $c, 2);
            $decimal = hexdec($hex);
            $plain  .= chr($decimal);
        }

        return ($this->plain = $plain);
    }
1124763
public function hex2rgb($hex, $returnString = FALSE)
    {
        $hex = str_replace('#', '', $hex);

        if(strlen($hex) == 3)
        {
            $r = hexdec(substr($hex,0,1).substr($hex,0,1));
            $g = hexdec(substr($hex,1,1).substr($hex,1,1));
            $b = hexdec(substr($hex,2,1).substr($hex,2,1));
        }
        else
        {
            $r = hexdec(substr($hex,0,2));
            $g = hexdec(substr($hex,2,2));
            $b = hexdec(substr($hex,4,2));
        }

        if($returnString)
        {
            return implode(', ', array($r, $g, $b));
        }

        return array($r, $g, $b);
    }
1326367
public function toHexString()
    {
        return '#' .
            $this->decToHex($this->r) .
            $this->decToHex($this->g) .
            $this->decToHex($this->b);
    }
302526
public static function hexToRgb($hex) {
    if (!self::validateHex($hex)) {
      throw new \InvalidArgumentException("'$hex' is not a valid hex value.");
    }

    // Ignore '#' prefixes.
    $hex = ltrim($hex, '#');

    // Convert shorthands like '#abc' to '#aabbcc'.
    if (strlen($hex) == 3) {
      $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
    }

    $c = hexdec($hex);

    return array(
      'red' => $c >> 16 & 0xFF,
      'green' => $c >> 8 & 0xFF,
      'blue' => $c & 0xFF,
    );
  }
309449
public static function bin2ascii($input)
    {
        $hextab = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $result = '';

        for ($x = 0; $x < strlen($input); $x++) {
            $result .= $hextab[ord($input[$x]) % 62];
        }

        return $result;
    }