{"record":{"id":"ece8d512a2b92619","repo":"PHPOffice/PhpSpreadsheet","slug":"num-ece8d5","errorCode":null,"errorMessage":"#NUM!","messagePattern":"#NUM!","errorType":"exception","errorClass":"PhpSpreadsheet\\Calculation\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Calculation/Engineering/ConvertBinary.php","lineNumber":158,"sourceCode":"            $value = self::validateValue($value);\n            $value = self::validateBinary($value);\n            $places = self::validatePlaces($places);\n        } catch (Exception $e) {\n            return $e->getMessage();\n        }\n\n        if (strlen($value) == 10 && $value[0] === '1') { //    Two's Complement\n            return str_repeat('7', 6) . strtoupper(decoct((int) bindec(\"11$value\")));\n        }\n        $octVal = (string) decoct((int) bindec($value));\n\n        return self::nbrConversionFormat($octVal, $places);\n    }\n\n    protected static function validateBinary(string $value): string\n    {\n        if ((strlen($value) > preg_match_all('/[01]/', $value)) || (strlen($value) > 10)) {\n            throw new Exception(ExcelError::NAN());\n        }\n\n        return $value;\n    }\n}\n","sourceCodeStart":140,"sourceCodeEnd":164,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Calculation/Engineering/ConvertBinary.php#L140-L164","documentation":"#NUM! from the binary-string validator used by BIN2DEC, BIN2HEX and BIN2OCT. ConvertBinary::validateBinary() throws Calculation\\Exception('#NUM!') when the (uppercased) value contains characters other than 0/1 or is longer than 10 characters - Excel binary numbers are at most 10 bits with two's-complement negatives.","triggerScenarios":"=BIN2DEC(\"102\"), =BIN2DEC(\"10 1\") (space fails), =BIN2DEC(\"0b101\") ('B' is not 0/1), =BIN2DEC(\"10101010101\") (11 chars); PHP calls ConvertBinary::toDecimal('1201').","commonSituations":"Binary strings with whitespace or '0b'/'b' prefixes from programming contexts; values copied from formatted cells; strings longer than 10 bits; hex or decimal digits passed by mistake to a BIN2* function.","solutions":["Normalise the input to /^[01]{1,10}$/ before calling (strip '0b', whitespace, underscores).","Truncate or reject >10-bit values; if you need wider integers, convert in PHP with bindec()/gmp and write the result.","Check results for '#NUM!' when binary strings come from users or imports."],"exampleFix":"// before\n$dec = ConvertBinary::toDecimal('0b1010'); // '#NUM!'\n\n// after\n$bin = preg_replace('/^0b/i', '', trim($input));\n$dec = preg_match('/^[01]{1,10}$/', $bin) ? ConvertBinary::toDecimal($bin) : bindec($bin);","handlingStrategy":"validation","validationCode":"$bin = preg_replace('/^0b/i', '', trim((string) $value));\nif (!preg_match('/^[01]{1,10}$/', $bin)) {\n    throw new \\InvalidArgumentException('value must be 1-10 binary digits');\n}\n$dec = ConvertBinary::toDecimal($bin);","typeGuard":"/** BIN2* input: 1-10 characters, digits 0/1 only. */\nfunction isValidBinaryString(mixed $v): bool\n{\n    return is_string($v) && preg_match('/^[01]{1,10}$/', $v) === 1;\n}","tryCatchPattern":null,"preventionTips":["Strip '0b' prefixes, underscores and whitespace from programming-style binary strings.","Reject >10-bit values up front; use bindec()/GMP for wider integers.","Negative numbers must be 10-character two's-complement strings (e.g. '1111111111'), not '-101'."],"tags":["phpspreadsheet","excel-formula","base-conversion","binary","pattern-validation"],"backgroundTag":"excel-num-error","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}