Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions src/PhpSpreadsheet/Calculation/Engineering.php
Original file line number Diff line number Diff line change
Expand Up @@ -1327,12 +1327,16 @@ public static function DECTOBIN($x, $places = null)
if (strlen($x) > preg_match_all('/[-0123456789.]/', $x, $out)) {
return Functions::VALUE();
}

$x = (string)floor($x);
if ($x < -512 || $x > 511) {
return Functions::NAN();
}

$r = decbin($x);
if (strlen($r) == 32) {
// Two's Complement
$r = substr($r, -10);
} elseif (strlen($r) >= 11) {
// Two's Complement
$r = substr($r, -10);
if (strlen($r) >= 11) {
return Functions::NAN();
}

Expand Down Expand Up @@ -1491,11 +1495,8 @@ public static function HEXTOBIN($x, $places = null)
if (strlen($x) > preg_match_all('/[0123456789ABCDEF]/', strtoupper($x), $out)) {
return Functions::NAN();
}
if (hexdec($x) > 0x1FF) {
return Functions::NAN();
}
$binVal = decbin(hexdec($x));
return substr(self::nbrConversionFormat($binVal, $places), -10);

return self::DECTOBIN(self::HEXTODEC($x), $places);
}


Expand Down Expand Up @@ -1529,9 +1530,14 @@ public static function HEXTODEC($x)
if (strlen($x) > preg_match_all('/[0123456789ABCDEF]/', strtoupper($x), $out)) {
return Functions::NAN();
}

if (strlen($x)> 10) {
return Functions::NAN();
}

$binX = '';
foreach (str_split($x) as $char) {
$binX .= str_pad(base_convert($char, 16, 2), 3, '0', STR_PAD_LEFT);
$binX .= str_pad(base_convert($char, 16, 2), 4, '0', STR_PAD_LEFT);
}
if (strlen($binX) == 40 && $binX[0] == '1') {
for ($i = 0; $i < 40; $i++) {
Expand Down Expand Up @@ -1587,11 +1593,14 @@ public static function HEXTOOCT($x, $places = null)
if (strlen($x) > preg_match_all('/[0123456789ABCDEF]/', strtoupper($x), $out)) {
return Functions::NAN();
}
$octVal = decoct(hexdec($x));

return self::nbrConversionFormat($octVal, $places);
}
$decimal = self::HEXTODEC($x);
if ($decimal < -536870912 || $decimal > 536870911) {
return Functions::NAN();
}

return self::DECTOOCT($decimal, $places);
}

/**
* OCTTOBIN
Expand Down Expand Up @@ -1639,9 +1648,8 @@ public static function OCTTOBIN($x, $places = null)
if (preg_match_all('/[01234567]/', $x, $out) != strlen($x)) {
return Functions::NAN();
}
$r = decbin(octdec($x));

return self::nbrConversionFormat($r, $places);
return self::DECTOBIN(self::OCTTODEC($x), $places);
}


Expand Down
5 changes: 0 additions & 5 deletions unitTests/Classes/src/Calculation/EngineeringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,6 @@ public function providerBIN2OCT()

/**
* @dataProvider providerDEC2BIN
* @group fail19
*/
public function testDEC2BIN()
{
Expand Down Expand Up @@ -521,7 +520,6 @@ public function providerDEC2OCT()

/**
* @dataProvider providerHEX2BIN
* @group fail19
*/
public function testHEX2BIN()
{
Expand All @@ -538,7 +536,6 @@ public function providerHEX2BIN()

/**
* @dataProvider providerHEX2DEC
* @group fail19
*/
public function testHEX2DEC()
{
Expand All @@ -555,7 +552,6 @@ public function providerHEX2DEC()

/**
* @dataProvider providerHEX2OCT
* @group fail19
*/
public function testHEX2OCT()
{
Expand All @@ -572,7 +568,6 @@ public function providerHEX2OCT()

/**
* @dataProvider providerOCT2BIN
* @group fail19
*/
public function testOCT2BIN()
{
Expand Down
4 changes: 3 additions & 1 deletion unitTests/rawTestData/Calculation/Engineering/DEC2BIN.data
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
357, "101100101"
1357, "#NUM!" // Too large
512, "#NUM!" // Too large
-513, "#NUM!" // Too small
9, 4, "1001"
9, 8, "00001001"
9, 6.75, "001001" // Leading places as a float
Expand All @@ -14,3 +15,4 @@
TRUE, "#VALUE!" // Non string
-100, "1110011100" // 2's Complement
-107, "1110010101" // 2's Complement
-512, "1000000000" // 2's Complement
8 changes: 6 additions & 2 deletions unitTests/rawTestData/Calculation/Engineering/HEX2BIN.data
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"FF", "11111111"
"1FF", "111111111"
"200", "#NUM!"
"FFFFFFFE00", "1000000000" // 2's Complement
"FFFFFFFDFF", "#NUM!" // 2's Complement
"01AB", "110101011"
"ABCD", "#NUM!"
"F6", "11110110"
Expand All @@ -9,5 +14,4 @@
"0", "0"
"G3579A", "#NUM!"
TRUE, "#VALUE!"
"-107", "#NUM!"
"FFFFFFFFFF", "1111111111" // 2's Complement
"-107", "#NUM!"
6 changes: 4 additions & 2 deletions unitTests/rawTestData/Calculation/Engineering/HEX2DEC.data
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
TRUE, "#VALUE!"
"-107", "#NUM!"
"A5", "165"
"FFFFFFFF5B", "-165"
"3DA408B9", "1034160313" // 2's Complement
"3DA408B9", "1034160313"
"FFFFFFFF5B", "-165" // 2's Complement
"FFFFFFFFFF", "-1" // 2's Complement
"1FFFFFFFFFF", "#NUM!" // Too large
4 changes: 4 additions & 0 deletions unitTests/rawTestData/Calculation/Engineering/OCT2BIN.data
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
TRUE, "#VALUE!"
"3579", "#NUM!"
"7777777000", "1000000000" // 2's Complement
"7777777777", "1111111111" // 2's Complement
"17777777777", "#NUM!" // Too small
"777", "111111111"
"1777", "#NUM!" // Too large