seq[uint8] => uint32
공부/Nim2019. 5. 25. 08:11
proc to_uint32(self: seq[uint8]): uint32 =
if self.len() != 4:
return 0
result = (cast[uint32](self[3]) shl 0) or
(cast[uint32](self[2]) shl 8) or
(cast[uint32](self[1]) shl 16) or
(cast[uint32](self[0]) shl 24)
proc to_uint8_seq(self: uint32): seq[uint8] =
result = @[]
result.add(cast[uint8](self shr 0))
result.add(cast[uint8](self shr 8))
result.add(cast[uint8](self shr 16))
result.add(cast[uint8](self shr 24))
when isMainModule:
let sample = @[0x00'u8, 0x00, 0x00, 0x10]
echo sample.to_uint32() # 16
echo sample.to_uint32().to_uint8_seq() # @[0, 0, 0, 16]