Reading and converting binary file 2 bits at a time
Gary Willoughby via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Aug 29 16:34:46 PDT 2015
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
More information about the Digitalmars-d-learn
mailing list