export toBytesBE, toBytesLE
This commit is contained in:
parent
3ef8d0027a
commit
19e0ac6268
16
stint/io.nim
16
stint/io.nim
|
@ -370,7 +370,7 @@ func dumpHex*(a: StInt or StUint, order: static[Endianness] = bigEndian): string
|
||||||
# --------------------------------------------------------
|
# --------------------------------------------------------
|
||||||
{.push raises: [], inline, noinit, gcsafe.}
|
{.push raises: [], inline, noinit, gcsafe.}
|
||||||
|
|
||||||
export fromBytes, toBytes
|
export fromBytes, toBytes, toBytesLE, toBytesBE
|
||||||
|
|
||||||
func readUintBE*[bits: static[int]](ba: openArray[byte]): StUint[bits] {.noinit, inline.}=
|
func readUintBE*[bits: static[int]](ba: openArray[byte]): StUint[bits] {.noinit, inline.}=
|
||||||
## Convert a big-endian array of (bits div 8) Bytes to an UInt[bits] (in native host endianness)
|
## Convert a big-endian array of (bits div 8) Bytes to an UInt[bits] (in native host endianness)
|
||||||
|
@ -426,14 +426,6 @@ func readIntBE*[bits: static[int]](ba: openArray[byte]): StInt[bits] {.noinit, i
|
||||||
## - A signed integer of the same size with `bits` bits
|
## - A signed integer of the same size with `bits` bits
|
||||||
result.impl = (typeof result.impl).fromBytesBE(ba)
|
result.impl = (typeof result.impl).fromBytesBE(ba)
|
||||||
|
|
||||||
func toByteArrayBE*[bits: static[int]](n: StInt[bits]): array[bits div 8, byte] {.noinit, inline.}=
|
|
||||||
## Convert a Int[bits] to to a big-endian array of bits div 8 bytes
|
|
||||||
## Input:
|
|
||||||
## - a signed integer
|
|
||||||
## Returns:
|
|
||||||
## - a big-endian array of the same size
|
|
||||||
result = n.impl.toBytesBE()
|
|
||||||
|
|
||||||
func fromBytesBE*(T: type StInt, ba: openArray[byte]): T {.noinit, inline.}=
|
func fromBytesBE*(T: type StInt, ba: openArray[byte]): T {.noinit, inline.}=
|
||||||
result = readIntBE[T.bits](ba)
|
result = readIntBE[T.bits](ba)
|
||||||
|
|
||||||
|
@ -462,4 +454,10 @@ func fromBytesLE*(T: type StInt, ba: openArray[byte]): T {.noinit, inline.}=
|
||||||
template initFromBytesLE*(x: var StInt, ba: openArray[byte]) =
|
template initFromBytesLE*(x: var StInt, ba: openArray[byte]) =
|
||||||
x = fromBytesLE(type x, ba)
|
x = fromBytesLE(type x, ba)
|
||||||
|
|
||||||
|
template toBytesLE*[bits: static[int]](n: StInt[bits]): array[bits div 8, byte] =
|
||||||
|
result = n.impl.toBytesLE()
|
||||||
|
|
||||||
|
template toBytesBE*[bits: static[int]](n: StInt[bits]): array[bits div 8, byte] =
|
||||||
|
result = n.impl.toBytesBE()
|
||||||
|
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
Loading…
Reference in New Issue