Reading and converting binary file 2 bits at a time
Rikki Cattermole via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Aug 27 02:07:48 PDT 2015
On 27/08/15 9:00 PM, 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
You do not use the std.file to prefix call read.
The bool type is one byte in size. A value of 0 is false, anything else
is true. But commonly defined as 1.
So if you want to read only one bit you will need to use e.g. bit shifts
and bitwise and operator.
I believe bitsSet may help you.
http://dlang.org/phobos/std_bitmanip.html#.bitsSet
More information about the Digitalmars-d-learn
mailing list