42 public static function toBinary($value, $places = null): string
46 $value = self::validateDecimal($value);
49 return $e->getMessage();
52 $value = (int) floor((
float) $value);
53 if ($value > self::LARGEST_BINARY_IN_DECIMAL || $value < self::SMALLEST_BINARY_IN_DECIMAL) {
61 return self::nbrConversionFormat(
$r, $places);
89 public static function toHex($value, $places = null): string
93 $value = self::validateDecimal($value);
96 return $e->getMessage();
99 $value = floor((
float) $value);
100 if ($value > self::LARGEST_HEX_IN_DECIMAL || $value < self::SMALLEST_HEX_IN_DECIMAL) {
103 $r = strtoupper(dechex((
int) $value));
104 $r = self::hex32bit($value,
$r);
106 return self::nbrConversionFormat(
$r, $places);
109 public static function hex32bit(
float $value,
string $hexstr,
bool $force =
false): string
111 if (PHP_INT_SIZE === 4 || $force) {
112 if ($value >= 2 ** 32) {
113 $quotient = (int) ($value / (2 ** 32));
115 return strtoupper(substr(
'0' . dechex($quotient), -2) . $hexstr);
117 if ($value < -(2 ** 32)) {
118 $quotient = 256 - (int) ceil((-$value) / (2 ** 32));
120 return strtoupper(substr(
'0' . dechex($quotient), -2) . substr(
"00000000$hexstr", -8));
155 public static function toOctal($value, $places = null): string
159 $value = self::validateDecimal($value);
162 return $e->getMessage();
165 $value = (int) floor((
float) $value);
166 if ($value > self::LARGEST_OCTAL_IN_DECIMAL || $value < self::SMALLEST_OCTAL_IN_DECIMAL) {
170 $r = substr(
$r, -10);
172 return self::nbrConversionFormat(
$r, $places);
177 if (strlen($value) > preg_match_all(
'/[-0123456789.]/', $value)) {
static hex32bit(float $value, string $hexstr, bool $force=false)
static toHex($value, $places=null)
toHex.
const LARGEST_BINARY_IN_DECIMAL
static toOctal($value, $places=null)
toOctal.
static validateDecimal(string $value)
static toBinary($value, $places=null)
toBinary.
const SMALLEST_BINARY_IN_DECIMAL
const SMALLEST_OCTAL_IN_DECIMAL
const SMALLEST_HEX_IN_DECIMAL
const LARGEST_OCTAL_IN_DECIMAL
const LARGEST_HEX_IN_DECIMAL
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.