On Thursday, 27 August 2015 at 09:26:55 UTC, rumbu wrote:
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

auto bytes = cast(ubyte[])read("binaryfile");
foreach(b; bytes)
{
    writeln((b & 0xC0) >> 6);  //bits 7, 6
    writeln((b & 0x30) >> 4);  //bits 5, 4
    writeln((b & 0x0C) >> 2);  //bits 3, 2
    writeln((b & 0x03));       //bits 1, 0
}

That's lovely, thank you. One quick question, the length of the file is not a multiple of the length of ubyte, but the cast still seems to work. Do you know how it converts a truncated final section?

Thanks again

Andrew

Reply via email to