August 29, 2015 Re: Reading and converting binary file 2 bits at a time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike James | On Saturday, 29 August 2015 at 21:50:12 UTC, Mike James wrote: > On Saturday, 29 August 2015 at 20:15:53 UTC, Marc Schütz wrote: >> Just cast to `Crumbs[]` directly: >> >> import std.bitmanip; >> import std.stdio; >> import std.file; >> >> struct Crumbs { >> mixin(bitfields!( >> ubyte, "one", 2, >> ubyte, "two", 2, >> ubyte, "three", 2, >> ubyte, "four", 2 >> )); >> } >> >> void main(string[] argv) >> { >> auto raw = read("binaryfile"); >> auto buffer = cast(Crumbs[]) raw; >> >> foreach (cmb; buffer) { >> writefln("Crumb one: %s", cmb.one); >> writefln("Crumb two: %s", cmb.two); >> writefln("Crumb three: %s", cmb.three); >> writefln("Crumb four: %s", cmb.four); >> } >> } > > I like that :-) But it might not be safe: http://forum.dlang.org/thread/ztefzijqhwrouzlagrpq@forum.dlang.org |
August 30, 2015 Re: Reading and converting binary file 2 bits at a time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On Saturday, 29 August 2015 at 23:34:47 UTC, Gary Willoughby wrote:
> But it might not be safe: http://forum.dlang.org/thread/ztefzijqhwrouzlagrpq@forum.dlang.org
That link just takes me to this thread here again.
|
August 30, 2015 Re: Reading and converting binary file 2 bits at a time | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | On Sunday, 30 August 2015 at 00:02:16 UTC, anonymous wrote: > On Saturday, 29 August 2015 at 23:34:47 UTC, Gary Willoughby wrote: >> But it might not be safe: http://forum.dlang.org/thread/ztefzijqhwrouzlagrpq@forum.dlang.org > > That link just takes me to this thread here again. Here's the correct link. http://forum.dlang.org/thread/sugxdshytelayxnsthwl@forum.dlang.org |
August 31, 2015 Re: Reading and converting binary file 2 bits at a time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | Thanks very much for all the help, your advice worked a treat. One final question, originally I was defining the struct inside the main loop and it was using 4 bytes per field rather than 2 bits, e.g.: import std.bitmanip; import std.stdio; struct Crumb1 { mixin(bitfields!( ubyte, "one", 2, ubyte, "two", 2, ubyte, "three", 2, ubyte, "four", 2)); } void main() { struct Crumb2 { mixin(bitfields!( ubyte, "one", 2, ubyte, "two", 2, ubyte, "three", 2, ubyte, "four", 2)); } writeln(Crumb1.sizeof, " ", Crumb2.sizeof); } outputs: 1 16 Is this correect behaviour? Andrew |
August 31, 2015 Re: Reading and converting binary file 2 bits at a time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Brown | On Monday, 31 August 2015 at 18:00:54 UTC, Andrew Brown wrote:
> Is this correect behaviour?
Yes, the reason is because the nested struct has a hidden member - a pointer to its stack context. This allows it to access variables from the surrounding local scope.
It adds 8 bytes on 64 bit cuz that's pointer size, and the other bytes are padding so the pointer is aligned on a word boundary.
If you change it to `static struct` inside the main function (or any other function), you'll find it then has the size of one again, since a static struct does not have access to outer variables, so it leaves that hidden pointer out.
|
Copyright © 1999-2021 by the D Language Foundation