struct
Python-like 'struct' for Nim
Summary
| Latest Version | Unknown |
|---|---|
| License | MIT |
| CI Status | Failing |
| Downloads | 0 |
| Last Indexed | 2026-07-21 05:24 |
Tags
Installation
nimble install struct
choosenim install struct
git clone https://github.com/OpenSystemsLab/struct.nim
OS Compatibility
| Platform | Linux | macOS | Windows | FreeBSD | OpenBSD | NetBSD | Android | iOS | WASM | Embedded |
|---|---|---|---|---|---|---|---|---|---|---|
| struct | ✓ | ✓ | ✓ | - | - | - | - | - | - | - |
Source
| Repository | https://github.com/OpenSystemsLab/struct.nim |
|---|---|
| Homepage | https://github.com/OpenSystemsLab/struct.nim |
| Registry Source | nimble_official |
README
struct.nim
Python-like 'struct' for Nim
This library is still under development, use it as yourown risk!
Format String
Byte Order
| Character | Byte 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:
| Format | C Type | Python Type | Nim Type | Size (bytes) |
| x | pad byte | no value | ||
| b | char | string of length 1 | char | 1 |
| ? | _Bool | bool | bool | 1 |
| h | short | integer | integerint16 | 2 |
| H | usigned short | integer | integeruint16 | 2 |
| i | int | integer | integerint32 | 4 |
| I | unsigned int | integer | integeruint32 | 4 |
| q | long long | integer | int64 | 8 |
| Q | unsigned long long | integer | uint64 | 8 |
| f | float | float | float32 | 4 |
| d | double | float | float64 | 8 |
| s | char[] | string | string |
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