norbert/lib/utils.nim

19 lines
606 B
Nim
Raw Normal View History

2022-02-02 23:12:03 +01:00
func toUint8*(l: char): uint8 =
2022-01-23 18:29:13 +01:00
return ord(l).uint8
2022-02-02 23:12:03 +01:00
func toUint16*(l: char, h: char): uint16 =
2022-01-23 18:29:13 +01:00
return ord(l).uint16 or (ord(h).uint16 shl 8);
2022-02-02 23:12:03 +01:00
func uint16ToString*(n: uint16): string =
return chr(n shr 8) & chr(n and 0b11111111)
func toUint32*(a: char, b: char, d: char, c: char): uint32 =
return toUint16(a, b).uint32 or (toUint16(d, c).uint32 shl 16)
func uint32ToString*(n: uint32): string =
return uint16ToString((n shr 16).uint16) & uint16ToString((n and 0b1111111111111111).uint16)
func sliceBit*(s: char, i: uint8): bool =
2022-01-23 18:29:13 +01:00
assert i < 8
return ((toUint8(s) shr (8 - i)) and 1) == 1