tsizeof3.nim 732 B

123456789101112131415161718192021222324252627282930
  1. discard """
  2. output: '''
  3. @[48, 57]
  4. '''
  5. """
  6. # bug #7238
  7. type ByteArrayBE*[N: static[int]] = array[N, byte]
  8. ## A byte array that stores bytes in big-endian order
  9. proc toByteArrayBE*[T: SomeInteger](num: T): ByteArrayBE[sizeof(T)]=
  10. ## Convert an integer (in native host endianness) to a big-endian byte array
  11. ## Notice the result type
  12. const N = T.sizeof
  13. for i in 0 ..< N:
  14. result[i] = byte((num shr ((N-1-i) * 8)) and high(int8))
  15. let a = 12345.toByteArrayBE
  16. echo a[^2 .. ^1] # to make it work on both 32-bit and 64-bit
  17. #---------------------------------------------------------------------
  18. type
  19. Payload = object
  20. something: int
  21. vals: UncheckedArray[int]
  22. static:
  23. doAssert(compiles(offsetOf(Payload, vals)))