ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ConvertOctal.php
Go to the documentation of this file.
1<?php
2
4
7
9{
41 public static function toBinary($value, $places = null): string
42 {
43 try {
45 $value = self::validateOctal($value);
47 } catch (Exception $e) {
48 return $e->getMessage();
49 }
50
51 return ConvertDecimal::toBinary(self::toDecimal($value), $places);
52 }
53
70 public static function toDecimal($value): string
71 {
72 try {
74 $value = self::validateOctal($value);
75 } catch (Exception $e) {
76 return $e->getMessage();
77 }
78
79 $binX = '';
80 foreach (str_split($value) as $char) {
81 $binX .= str_pad(decbin((int) $char), 3, '0', STR_PAD_LEFT);
82 }
83 if (strlen($binX) == 30 && $binX[0] == '1') {
84 for ($i = 0; $i < 30; ++$i) {
85 $binX[$i] = ($binX[$i] == '1' ? '0' : '1');
86 }
87
88 return (string) ((bindec($binX) + 1) * -1);
89 }
90
91 return (string) bindec($binX);
92 }
93
120 public static function toHex($value, $places = null): string
121 {
122 try {
124 $value = self::validateOctal($value);
126 } catch (Exception $e) {
127 return $e->getMessage();
128 }
129
130 $hexVal = strtoupper(dechex((int) self::toDecimal($value)));
131 $hexVal = (PHP_INT_SIZE === 4 && strlen($value) === 10 && $value[0] >= '4') ? "FF$hexVal" : $hexVal;
132
133 return self::nbrConversionFormat($hexVal, $places);
134 }
135
136 protected static function validateOctal(string $value): string
137 {
138 $numDigits = (int) preg_match_all('/[01234567]/', $value);
139 if (strlen($value) > $numDigits || $numDigits > 10) {
140 throw new Exception(Functions::NAN());
141 }
142
143 return $value;
144 }
145}
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 flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
Definition: Functions.php:649
$i
Definition: disco.tpl.php:19