AbstractEndianness.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /*
  3. * This file is a part of "charcoal-dev/buffers" package.
  4. * https://github.com/charcoal-dev/buffers
  5. *
  6. * Copyright (c) Furqan A. Siddiqui <hello@furqansiddiqui.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code or visit following link:
  10. * https://github.com/charcoal-dev/buffers/blob/master/LICENSE
  11. */
  12. declare(strict_types=1);
  13. namespace Charcoal\Buffers\ByteOrder;
  14. use Charcoal\Buffers\ByteOrder;
  15. /**
  16. * Class AbstractEndianness
  17. * @package Charcoal\Buffers\ByteOrder
  18. */
  19. abstract class AbstractEndianness
  20. {
  21. /**
  22. * Constructor is disabled, no instances should be necessary
  23. */
  24. private function __construct()
  25. {
  26. }
  27. /**
  28. * Packs an 8-bit integer
  29. * @param int $n
  30. * @return string
  31. */
  32. public static function PackUInt8(int $n): string
  33. {
  34. ByteOrder::CheckUInt32($n, 1); // Verify that argument int can be packed in a single byte
  35. return chr($n);
  36. }
  37. /**
  38. * Unpacks an 8-bit integer
  39. * @param string $bn
  40. * @return int
  41. */
  42. public static function UnpackUInt8(string $bn): int
  43. {
  44. return ord($bn);
  45. }
  46. }