41 public static function toBinary($value, $places = null): string
45 $value = self::validateOctal($value);
48 return $e->getMessage();
74 $value = self::validateOctal($value);
76 return $e->getMessage();
80 foreach (str_split($value) as $char) {
81 $binX .= str_pad(decbin((
int) $char), 3,
'0', STR_PAD_LEFT);
83 if (strlen($binX) == 30 && $binX[0] ==
'1') {
84 for (
$i = 0;
$i < 30; ++
$i) {
85 $binX[
$i] = ($binX[
$i] ==
'1' ?
'0' :
'1');
88 return (
string) ((bindec($binX) + 1) * -1);
91 return (
string) bindec($binX);
120 public static function toHex($value, $places = null): string
124 $value = self::validateOctal($value);
127 return $e->getMessage();
130 $hexVal = strtoupper(dechex((
int) self::toDecimal($value)));
131 $hexVal = (PHP_INT_SIZE === 4 && strlen($value) === 10 && $value[0] >=
'4') ?
"FF$hexVal" : $hexVal;
133 return self::nbrConversionFormat($hexVal, $places);
138 $numDigits = (int) preg_match_all(
'/[01234567]/', $value);
139 if (strlen($value) > $numDigits || $numDigits > 10) {
static toBinary($value, $places=null)
toBinary.
static toBinary($value, $places=null)
toBinary.
static toDecimal($value)
toDecimal.
static toHex($value, $places=null)
toHex.
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
static validateOctal(string $value)