ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ConvertDecimal.php
Go to the documentation of this file.
1<?php
2
4
7
9{
10 const LARGEST_OCTAL_IN_DECIMAL = 536870911;
11 const SMALLEST_OCTAL_IN_DECIMAL = -536870912;
14 const LARGEST_HEX_IN_DECIMAL = 549755813887;
15 const SMALLEST_HEX_IN_DECIMAL = -549755813888;
16
42 public static function toBinary($value, $places = null): string
43 {
44 try {
46 $value = self::validateDecimal($value);
48 } catch (Exception $e) {
49 return $e->getMessage();
50 }
51
52 $value = (int) floor((float) $value);
53 if ($value > self::LARGEST_BINARY_IN_DECIMAL || $value < self::SMALLEST_BINARY_IN_DECIMAL) {
54 return Functions::NAN();
55 }
56
57 $r = decbin($value);
58 // Two's Complement
59 $r = substr($r, -10);
60
61 return self::nbrConversionFormat($r, $places);
62 }
63
89 public static function toHex($value, $places = null): string
90 {
91 try {
93 $value = self::validateDecimal($value);
95 } catch (Exception $e) {
96 return $e->getMessage();
97 }
98
99 $value = floor((float) $value);
100 if ($value > self::LARGEST_HEX_IN_DECIMAL || $value < self::SMALLEST_HEX_IN_DECIMAL) {
101 return Functions::NAN();
102 }
103 $r = strtoupper(dechex((int) $value));
104 $r = self::hex32bit($value, $r);
105
106 return self::nbrConversionFormat($r, $places);
107 }
108
109 public static function hex32bit(float $value, string $hexstr, bool $force = false): string
110 {
111 if (PHP_INT_SIZE === 4 || $force) {
112 if ($value >= 2 ** 32) {
113 $quotient = (int) ($value / (2 ** 32));
114
115 return strtoupper(substr('0' . dechex($quotient), -2) . $hexstr);
116 }
117 if ($value < -(2 ** 32)) {
118 $quotient = 256 - (int) ceil((-$value) / (2 ** 32));
119
120 return strtoupper(substr('0' . dechex($quotient), -2) . substr("00000000$hexstr", -8));
121 }
122 if ($value < 0) {
123 return "FF$hexstr";
124 }
125 }
126
127 return $hexstr;
128 }
129
155 public static function toOctal($value, $places = null): string
156 {
157 try {
159 $value = self::validateDecimal($value);
161 } catch (Exception $e) {
162 return $e->getMessage();
163 }
164
165 $value = (int) floor((float) $value);
166 if ($value > self::LARGEST_OCTAL_IN_DECIMAL || $value < self::SMALLEST_OCTAL_IN_DECIMAL) {
167 return Functions::NAN();
168 }
169 $r = decoct($value);
170 $r = substr($r, -10);
171
172 return self::nbrConversionFormat($r, $places);
173 }
174
175 protected static function validateDecimal(string $value): string
176 {
177 if (strlen($value) > preg_match_all('/[-0123456789.]/', $value)) {
178 throw new Exception(Functions::VALUE());
179 }
180
181 return $value;
182 }
183}
An exception for terminatinating execution or to throw for unit testing.
static nbrConversionFormat(string $value, ?int $places)
Formats a number base string value with leading zeroes.
Definition: ConvertBase.php:53
static hex32bit(float $value, string $hexstr, bool $force=false)
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
Definition: Functions.php:649
$r
Definition: example_031.php:79