HEX2BIN.php 1017 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  3. require __DIR__ . '/../../Header.php';
  4. $category = 'Engineering';
  5. $functionName = 'HEX2BIN';
  6. $description = 'Converts a hexadecimal number to binary';
  7. $helper->titles($category, $functionName, $description);
  8. // Create new PhpSpreadsheet object
  9. $spreadsheet = new Spreadsheet();
  10. $worksheet = $spreadsheet->getActiveSheet();
  11. // Add some data
  12. $testData = [
  13. [3],
  14. [8],
  15. [42],
  16. [99],
  17. ['A2'],
  18. ['F0'],
  19. ['100'],
  20. ['128'],
  21. ['1AB'],
  22. ['1FF'],
  23. ];
  24. $testDataCount = count($testData);
  25. $worksheet->fromArray($testData, null, 'A1', true);
  26. for ($row = 1; $row <= $testDataCount; ++$row) {
  27. $worksheet->setCellValue('B' . $row, '=HEX2BIN(A' . $row . ')');
  28. }
  29. // Test the formulae
  30. for ($row = 1; $row <= $testDataCount; ++$row) {
  31. $helper->log(sprintf(
  32. '(B%d): Hexadecimal %s is binary %s',
  33. $row,
  34. $worksheet->getCell('A' . $row)->getValue(),
  35. $worksheet->getCell('B' . $row)->getCalculatedValue(),
  36. ));
  37. }