summary refs log tree commit diff stats
path: root/tests/misc/tsizeof3.nim
blob: e04ce8a0c1b41b1f37e8cad1ea3fec2f06cf14c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
discard """
output: '''
[0, 0, 0, 0, 0, 0, 48, 57]
'''
"""
# bug #7238

type ByteArrayBE*[N: static[int]] = array[N, byte]
  ## A byte array that stores bytes in big-endian order

proc toByteArrayBE*[T: SomeInteger](num: T): ByteArrayBE[sizeof(T)]=
  ## Convert an integer (in native host endianness) to a big-endian byte array
  ## Notice the result type
  const N = T.sizeof
  for i in 0 ..< N:
    result[i] = byte(num shr ((N-1-i) * 8))

let a = 12345.toByteArrayBE
echo a