tsizeof3.nim 477 B

1234567891011121314151617181920
  1. discard """
  2. output: '''
  3. [0, 0, 0, 0, 0, 0, 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))
  15. let a = 12345.toByteArrayBE
  16. echo a