Reading bytes and converting to int

joao joao.rab at hotmail.com
Sat Aug 25 16:25:04 PDT 2012


On Saturday, 25 August 2012 at 20:58:47 UTC, Jonathan M Davis 
wrote:
> On Saturday, August 25, 2012 22:49:18 joao wrote:
>> Ok, so I tried both ways and gave errors:
>> import std.stdio, std.cstream, std.system, std.bitmanip;
>> 
>> File f = File("filename");
>> auto buf = f.rawRead(new ubyte[4]);
>> auto i = peek!(uint, Endian.littleEndian)(buf);
>> 
>> std.stdio.File conflicts with std.stream.File
>
> Then either don't import both or use the full import.
>
> auto file = std.stdio.File("filename");
>
> But I wouldn't advise using std.stream for anything unless you 
> have to. As the
> note in its documentation states, it's considered out-of-date 
> and will be
> replaced.
>
>> and
>> 
>> auto file = File("filename");
>> auto buffer = ubyte[](4);
>> buffer = file.rawRead(buffer);
>> auto i = peek!(uint, Endian.littleEndian)(buffer);
>> 
>> found '[' when expecting '.' following ubyte and ']' when
>> expecting identifier following 'ubyte'
>
> It's missing new. My mistake.
>
> auto buffer = new ubyte[](4);
>
> or as David pointed out, you get just allocate the array in the 
> rawRead call:
>
> auto buf = file.rawRead(new ubyte[](4));
>
> - Jonathan M Davis

It worked! Thanks.
Just one more question, I and to do the reverse. Like 4132 => 
"\x24\x10"


More information about the Digitalmars-d-learn mailing list