See Release Notes
Long Term Support Release
Differences Between: [Versions 400 and 401]
1 <?php 2 3 namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering; 4 5 use PhpOffice\PhpSpreadsheet\Calculation\Exception; 6 use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; 7 8 class ConvertOctal extends ConvertBase 9 { 10 /** 11 * toBinary. 12 * 13 * Return an octal value as binary. 14 * 15 * Excel Function: 16 * OCT2BIN(x[,places]) 17 * 18 * @param array|string $value The octal number you want to convert. Number may not 19 * contain more than 10 characters. The most significant 20 * bit of number is the sign bit. The remaining 29 bits 21 * are magnitude bits. Negative numbers are represented 22 * using two's-complement notation. 23 * If number is negative, OCT2BIN ignores places and returns 24 * a 10-character binary number. 25 * If number is negative, it cannot be less than 7777777000, 26 * and if number is positive, it cannot be greater than 777. 27 * If number is not a valid octal number, OCT2BIN returns 28 * the #NUM! error value. 29 * If OCT2BIN requires more than places characters, it 30 * returns the #NUM! error value. 31 * Or can be an array of values 32 * @param array|int $places The number of characters to use. If places is omitted, 33 * OCT2BIN uses the minimum number of characters necessary. 34 * Places is useful for padding the return value with 35 * leading 0s (zeros). 36 * If places is not an integer, it is truncated. 37 * If places is nonnumeric, OCT2BIN returns the #VALUE! 38 * error value. 39 * If places is negative, OCT2BIN returns the #NUM! error 40 * value. 41 * Or can be an array of values 42 * 43 * @return array|string Result, or an error 44 * If an array of numbers is passed as an argument, then the returned result will also be an array 45 * with the same dimensions 46 */ 47 public static function toBinary($value, $places = null) 48 { 49 if (is_array($value) || is_array($places)) { 50 return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $places); 51 } 52 53 try { 54 $value = self::validateValue($value); 55 $value = self::validateOctal($value); 56 $places = self::validatePlaces($places); 57 } catch (Exception $e) { 58 return $e->getMessage(); 59 } 60 61 return ConvertDecimal::toBinary(self::toDecimal($value), $places); 62 } 63 64 /** 65 * toDecimal. 66 * 67 * Return an octal value as decimal. 68 * 69 * Excel Function: 70 * OCT2DEC(x) 71 * 72 * @param array|string $value The octal number you want to convert. Number may not contain 73 * more than 10 octal characters (30 bits). The most significant 74 * bit of number is the sign bit. The remaining 29 bits are 75 * magnitude bits. Negative numbers are represented using 76 * two's-complement notation. 77 * If number is not a valid octal number, OCT2DEC returns the 78 * #NUM! error value. 79 * Or can be an array of values 80 * 81 * @return array|string Result, or an error 82 * If an array of numbers is passed as an argument, then the returned result will also be an array 83 * with the same dimensions 84 */ 85 public static function toDecimal($value) 86 { 87 if (is_array($value)) { 88 return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value); 89 } 90 91 try { 92 $value = self::validateValue($value); 93 $value = self::validateOctal($value); 94 } catch (Exception $e) { 95 return $e->getMessage(); 96 } 97 98 $binX = ''; 99 foreach (str_split($value) as $char) { 100 $binX .= str_pad(decbin((int) $char), 3, '0', STR_PAD_LEFT); 101 } 102 if (strlen($binX) == 30 && $binX[0] == '1') { 103 for ($i = 0; $i < 30; ++$i) { 104 $binX[$i] = ($binX[$i] == '1' ? '0' : '1'); 105 } 106 107 return (string) ((bindec($binX) + 1) * -1); 108 } 109 110 return (string) bindec($binX); 111 } 112 113 /** 114 * toHex. 115 * 116 * Return an octal value as hex. 117 * 118 * Excel Function: 119 * OCT2HEX(x[,places]) 120 * 121 * @param array|string $value The octal number you want to convert. Number may not contain 122 * more than 10 octal characters (30 bits). The most significant 123 * bit of number is the sign bit. The remaining 29 bits are 124 * magnitude bits. Negative numbers are represented using 125 * two's-complement notation. 126 * If number is negative, OCT2HEX ignores places and returns a 127 * 10-character hexadecimal number. 128 * If number is not a valid octal number, OCT2HEX returns the 129 * #NUM! error value. 130 * If OCT2HEX requires more than places characters, it returns 131 * the #NUM! error value. 132 * Or can be an array of values 133 * @param array|int $places The number of characters to use. If places is omitted, OCT2HEX 134 * uses the minimum number of characters necessary. Places is useful 135 * for padding the return value with leading 0s (zeros). 136 * If places is not an integer, it is truncated. 137 * If places is nonnumeric, OCT2HEX returns the #VALUE! error value. 138 * If places is negative, OCT2HEX returns the #NUM! error value. 139 * Or can be an array of values 140 * 141 * @return array|string Result, or an error 142 * If an array of numbers is passed as an argument, then the returned result will also be an array 143 * with the same dimensions 144 */ 145 public static function toHex($value, $places = null) 146 { 147 if (is_array($value) || is_array($places)) { 148 return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $places); 149 } 150 151 try { 152 $value = self::validateValue($value); 153 $value = self::validateOctal($value); 154 $places = self::validatePlaces($places); 155 } catch (Exception $e) { 156 return $e->getMessage(); 157 } 158 159 $hexVal = strtoupper(dechex((int) self::toDecimal($value))); 160 $hexVal = (PHP_INT_SIZE === 4 && strlen($value) === 10 && $value[0] >= '4') ? "FF{$hexVal}" : $hexVal; 161 162 return self::nbrConversionFormat($hexVal, $places); 163 } 164 165 protected static function validateOctal(string $value): string 166 { 167 $numDigits = (int) preg_match_all('/[01234567]/', $value); 168 if (strlen($value) > $numDigits || $numDigits > 10) { 169 throw new Exception(ExcelError::NAN()); 170 } 171 172 return $value; 173 } 174 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body