Hello, I know C and Python reasonably well. I'm trying to get into Nimrod which looks very promising...
I'm trying to write a small program that need to unpack some binary data from a file... I'm looking for something very much like the struct module of Python.
I have a tough time getting started, finding how to approach it the "nimrod way"... The docs are good I think if you know a bit of nimrod already, but a bit dry for the complete newcomer...
Can somebody help me out? Thanks in advance.
You don't need a python-like struct module because Nimrod supports C-like structs, you need to declare an object {.pure.} to describe the structure you're trying to 'unpack'. e.g.
type
TMyBinaryHeader = object {.pure.}
magic: int64
someValue: uint16
....
Also see 'align' pragma.
For now, you need to implement the byte swapping on your own like so:
proc toBigEndian*(x: int32): int32 =
when cpuEndian == bigEndian: result = x
else: result = (x shr 24'i32) or
(x shr 8'i32 and 0xff00'i32) or
(x shl 8'i32 and 0xff0000'i32) or
(x shl 24'i32)
Since is a common feature request, please implement an endians module and make a pull request.