View mode: basic / threaded / horizontal-split · Log in · Help
August 25, 2012
Re: Reading bytes and converting to int
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
August 25, 2012
Re: Reading bytes and converting to int
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"
August 26, 2012
Re: Reading bytes and converting to int
On Sunday, August 26, 2012 01:25:04 joao wrote:
> It worked! Thanks.
> Just one more question, I and to do the reverse. Like 4132 =>
> "\x24\x10"

Use std.bitmanip.write:

http://dlang.org/phobos/std_bitmanip.html#write

- Jonathan M Davis
August 26, 2012
Re: Reading bytes and converting to int
On Saturday, 25 August 2012 at 20:58:47 UTC, Jonathan M Davis 
wrote:
> auto buf = file.rawRead(new ubyte[](4));

Could we somehow skip making the temporary buffer, and read from 
the file directly into an existing variable. Can we make a slice 
of one element that points to an existing value.

import std.stdio;

struct BigValue // has no indirection
{
    int  m_value1;
    int  m_value2;
    long m_value3;
    // ...
}

BigValue g_bigValue;

void fun()
{
    auto file = File("filename");

    // How to create a slice of size 1 which references 
g_bigValue?
    BigValue[] refToBigValue /* = ? */ ;

    buffer = file.rawRead(refToBigValue);
}
August 26, 2012
Re: Reading bytes and converting to int
On 08/26/2012 04:19 PM, Tommi wrote:
> On Saturday, 25 August 2012 at 20:58:47 UTC, Jonathan M Davis wrote:
>> auto buf = file.rawRead(new ubyte[](4));
>
> Could we somehow skip making the temporary buffer, and read from the
> file directly into an existing variable. Can we make a slice of one
> element that points to an existing value.
>
> import std.stdio;
>
> struct BigValue // has no indirection
> {
>      int  m_value1;
>      int  m_value2;
>      long m_value3;
>      // ...
> }
>
> BigValue g_bigValue;
>
> void fun()
> {
>      auto file = File("filename");
>
>      // How to create a slice of size 1 which references g_bigValue?
>      BigValue[] refToBigValue /* = ? */ ;

auto refToBigValue = (&g_bigValue)[0..1];

>
>      buffer = file.rawRead(refToBigValue);
> }
August 26, 2012
Re: Reading bytes and converting to int
On Sunday, 26 August 2012 at 15:18:27 UTC, Timon Gehr wrote:
>
> auto refToBigValue = (&g_bigValue)[0..1];
>

Thanks. Oughta read the f***ing manual instead of glancing 
through it:
http://dlang.org/arrays.html
Next ›   Last »
1 2
Top | Discussion index | About this forum | D home