On Thursday, 27 August 2015 at 09:00:02 UTC, Andrew Brown wrote:
Hi,

I need to read a binary file, and then process it two bits at a time. But I'm a little stuck on the first step. So far I have:

import std.file;
import std.stdio;

void main(){
  auto f = std.file.read("binaryfile");
  auto g = cast(bool[]) f;
  writeln(g);
}

but all the values of g then are just true, could you tell me what I'm doing wrong? I've also looked at the bitmanip module, I couldn't get it to help, but is that the direction I should be looking?

Thanks very much

Andrew

How about...

module main;

import std.bitmanip;
import std.stdio;

struct Crumbs {
    @property ref ubyte whole() {
        return m_whole;
    }

    union {
        private ubyte m_whole;
        mixin(bitfields!(
            ubyte, "one",   2,
            ubyte, "two",   2,
            ubyte, "three", 2,
            ubyte, "four",  2
        ));
    }
}

void main(string[] argv)
{
    ubyte[] buffer = [123, 12, 126, 244, 35];
    Crumbs cmb;

    foreach (octet; buffer) {
        cmb.whole = octet;

        writefln("Crumb:       %08b", octet);
        writefln("Crumb one:   %s", cmb.one);
        writefln("Crumb two:   %s", cmb.two);
        writefln("Crumb three: %s", cmb.three);
        writefln("Crumb four:  %s", cmb.four);
    }
}


Regards, Mike.

Reply via email to