BC utility for directly converting
a decimal number to hexadecimal.

@param  string $dec Number to convert to hex.
@return string      Hex form of the number.
556242
public function convertDecToHex($dec)
    {
        if (strlen($dec) < 5) {
            return dechex($dec);
        }

        list($remain, $last) = array(bcdiv(bcsub($dec, $last), '16'), bcmod($dec, '16'));

        return ($remain == 0) ? dechex($last) : $this->convertDecToHex($remain) . dechex($last);
    }
813054
function(dec) {
         var r = parseInt(dec,10).toString(16);
         if (r.length == 1) r = "0"+r;
         return r;
      }
879142
public function getIsDecimalEven( $precision = false )
	{
		if ( ! $this->getIsDecimal() ) {
			return false;
		}

		if ( $precision === false ) $precision = $this->scale( $this->_value );
		if ( $precision < 0 ) $precision = $precision = 0;

		$decimalPart = $this->getDecimalPart();
		if ( $precision >= strlen( $decimalPart ) ) return false;

		// Get the portion of the decimal part affected by the scale value
		$decimalPart = substr( $decimalPart, $precision );
		return $decimalPart[0] % 2 === 0;
	}
1162452
private function hex($ascii)
    {
        $str = dechex(ord($ascii));
        if (!isset($str[1])) {
            $str = '0' . $str; //补位
        }
        return $str;
    }
696411
public static function dec2big(string $number): string
    {
    	static::checkExtensionOrFail();

        $alphabet = static::NUMBERS;
        $number = trim((string)$number);

        if (strlen($number) == 0) {
            throw new Exception('Got empty number for dec2big, can not proceed');
        }

        $mod = (string)count($alphabet);
        $s = '';

        do {
            $x = bcdiv($number, $mod, 0);
            $left = bcmod($number, $mod);
            $char = $alphabet[$left];
            $s = $char . $s;

            $number = $x;
        } while ($x != '0');

        return $s;
    }
928689
public function decr($key, $decrement = 1) {
        if ($decrement === 1) {
            return $this->send(['decr', $key]);
        }

        return $this->send(['decrby', $key, $decrement]);
    }
485730
public static function fromString($number)
    {
        $decimalSeparatorPosition = strpos($number, '.');
        if ($decimalSeparatorPosition === false) {
            return new self($number, '');
        }

        return new self(
            substr($number, 0, $decimalSeparatorPosition),
            rtrim(substr($number, $decimalSeparatorPosition + 1), '0')
        );
    }
1078746
public function format($decimals = 2, $decPoint = '.', $thousandSep = ',')
    {
        return number_format($this->value, $decimals, $decPoint, $thousandSep);
    }
829331
public function dec($key) {
        $v = new DecimalValidator($this->shell->_unsafeVar[$key] ?? null, $this->shell);
        $v->_isUndefined = !array_key_exists($key, $this->shell->_unsafeVar);
        $this->shell->subValidators[$key] = $v;
        return $v;
    }
445366
public function formatBytes($bytes, $dec = 2)
    {
        $unitpow = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
        $pos = 0;
        while ($bytes >= 1024) {
            $bytes /= 1024;
            ++$pos;
        }

        return round($bytes, $dec).' '.$unitpow[$pos];
    }
515525
private function number_format($number, $decimal_precision = 0, $decimals_separator = '.', $thousands_separator = ',') {
        $number = explode('.', str_replace(' ', '', $number));
        $number[0] = str_split(strrev($number[0]), 3);
        $total_segments = count($number[0]);
        for ($i = 0; $i < $total_segments; $i++) {
            $number[0][$i] = strrev($number[0][$i]);
        }
        $number[0] = implode($thousands_separator, array_reverse($number[0]));
        if (!empty($number[1])) {
           $number[1] = round($number[1], $decimal_precision);
        }
        return implode($decimals_separator, $number);
    }
262178
private function intToHex($i)
    {
        // Sanitize the input.
        $i = (int) $i;

        // Get the first character of the hexadecimal string if there is one.
        $j = (int) ($i / 16);

        if ($j === 0) {
            $s = ' ';
        } else {
            $s = strtoupper(dechex($j));
        }

        // Get the second character of the hexadecimal string.
        $k = $i - $j * 16;
        $s = $s . strtoupper(dechex($k));

        return $s;
    }
1157801
public function setFormat($decimals = 0, $dec_point = '.', $thousands_sep = ' ')
	{
		$this->number_format = [$decimals, $dec_point, $thousands_sep];

		return $this;
	}
318591
public static function hex2asc($string)
    {
        $string = preg_replace_callback(
            "/\\\[0-9A-Fa-f]{2}/",
            function ($matches) {
                return chr(hexdec($matches[0]));
            },
            $string
        );
        return $string;
    }
732942
public static function getNumberBaseFromString($value)
    {
        if (substr($value, 0, 1) == 'b')
            return self::BINARY;

        if (substr($value, 0, 1) == 'o')
            return self::OCTAL;

        if (substr($value, 0, 2) == '0x')
            return self::HEXADECIMAL;

        if (is_numeric($value))
            return self::DECIMAL;

        throw new \Exception ("not a number ".$value);
    }
384438
public static function format($value)
    {
        $value = Utils::toString($value);

        if (Utils::isZeroPrefixed($value)) {
            // test hex with zero ahead, hardcode 0x0
            if ($value === '0x0' || strpos($value, '0x0') !== 0) {
                return $value;
            }
            $hex = preg_replace('/^0x0+(?!$)/', '', $value);
        } else {
            $bn = Utils::toBn($value);
            $hex = $bn->toHex(true);
        }
        if (empty($hex)) {
            $hex = '0';
        } else {
            $hex = preg_replace('/^0+(?!$)/', '', $hex);
        }
        return '0x' . $hex;
    }
1178514
private static function padRgbaComponent($component)
    {
        $result = \dechex($component);

        if (2 > \strlen($result)) {
            $result = '0' . $result;
        }

        return $result;
    }