Reading and converting binary file 2 bits at a time

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Aug 29 13:15:51 PDT 2015


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);
         }
     }


More information about the Digitalmars-d-learn mailing list