src/intobject/int_bytes

Source   Edit  

Types

IntObjectToBytesError {.pure.} = enum
  Ok, LengthNegative = "length argument must be non-negative",
  NegativeToUnsigned = "can\'t convert negative int to unsigned",
  TooBigToConvert = "int too big to convert"
Source   Edit  

Procs

proc newInt(bytes: PyBytes; endianness: Endianness; signed = false): NimInt {.
    ...raises: [], tags: [], forbids: [].}
Source   Edit  
proc newInt[T: uint8 | int8](bytes: openArray[T]; endianness: Endianness;
                             signed = false): NimInt
Source   Edit  
func parseByteOrder(byteorder: string): Endianness {....raises: [ValueError],
    tags: [], forbids: [].}
Source   Edit  
proc to_bytes[T: char | uint8 | int8](self: NimInt; length: int;
                                      endianness: Endianness; signed = false;
                                      res: var seq[T]): IntObjectToBytesError
EXT. Source   Edit  
proc tobytes(self: NimInt; length = 1; byteorder: string = "big"; signed = false): seq[
    byte] {....raises: [ValueError], tags: [], forbids: [].}
Warning: for compatibility with Python, the default value of length is 1. And if the length bytes is not enough to hold the integer, it raises ValueError

Example:

## To convert with a suitable length, you can use the `length` argument:
import intobject
let i = newInt("\x01\x02", bigEndian)
assert [1u8, 2] == i.to_bytes(length = i.byteCount)

doAssertRaises ValueError:
  discard i.to_bytes()
Source   Edit  
proc tobytes(self: NimInt; length: int; endianness: Endianness; signed = false): seq[
    byte] {....raises: [ValueError], tags: [], forbids: [].}
Source   Edit  
proc tochars(self: NimInt; length = 1; byteorder: string = "big"; signed = false): seq[
    char] {....raises: [ValueError], tags: [], forbids: [].}
Warning: for compatibility with Python, the default value of length is 1. And if the length bytes is not enough to hold the integer, it raises ValueError
Source   Edit  
proc tochars(self: NimInt; length: int; endianness: Endianness; signed = false): seq[
    char] {....raises: [ValueError], tags: [], forbids: [].}
Source   Edit