No description
Find a file
2021-10-06 21:36:14 +07:00
.github Create FUNDING.yml 2021-10-01 08:03:52 +07:00
src fix wrong offset for pack_32 and pack_64 with repeat, closes #7 2021-10-06 21:36:14 +07:00
tests fix wrong offset for pack_32 and pack_64 with repeat, closes #7 2021-10-06 21:36:14 +07:00
.gitignore Update README 2015-05-25 03:13:01 +07:00
LICENSE Initial commit 2015-05-21 02:53:52 +07:00
README.md Update README 2015-09-08 16:11:01 +07:00
struct.nimble fix wrong offset for pack_32 and pack_64 with repeat, closes #7 2021-10-06 21:36:14 +07:00

struct.nim

Python-like 'struct' for Nim

This library is still under development, use it as yourown risk!

Format String

Byte Order

CharacterByte order
@native
=native
<little-endian
>big-endian
!network (= big-endian)

Notes:

  • Unlike Python -byte-order can specified once as first character, with this implementation, you can change byte-order anywhere and anytime you want

Format Characters:

integerintegerintegerinteger
FormatC TypePython TypeNim TypeSize (bytes)
xpad byteno value
bcharstring of length 1char1
?_Boolboolbool1
hshortintegerint162
Husigned shortintegeruint162
iintintegerint324
Iunsigned intintegeruint324
qlong longintegerint648
Qunsigned long longintegeruint648
ffloatfloatfloat324
ddoublefloatfloat648
schar[]stringstring

Notes:

  • Format character can has a number prefix, you can use "3?" instead of "???" for pack/unpack three bool value
  • For string , number prefix is the length of value

Usage

# >>> from struct import *
import struct

# >>> pack('hhi', 1, 2, 3)
var output =  pack("hhi", 1, 2, 3)

# alternative way to pack
# output = pack("hhi", newStructInt(1), newStructInt(1), newStructInt(3))

# >>> unpack('hhi', '\x00\x01\x00\x02\x00\x00\x00\x03')
var result = unpack("hhi", output);
echo result[0].getShort
echo result[1].getShort
echo result[2].getInt