Reading and converting binary file 2 bits at a time

Mike James via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Aug 29 14:50:11 PDT 2015


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 :-)



More information about the Digitalmars-d-learn mailing list