digitalmars.D.learn - bytes into integer, need help
- James (10/10) Nov 05 2008 im having problem in code below, while making my own file handler. the d...
- Steven Schveighoffer (19/32) Nov 06 2008 I think you meant this?
- Denis Koroskin (4/41) Nov 06 2008 Good solution also cares about endianess.
- James (2/46) Nov 06 2008 thanks, exactly what im looking for. endianness isnt a problem at this p...
im having problem in code below, while making my own file handler. the data
stored in buffer, so i can recall portion of data in diff format and length
like int,short,etc. but im stuck with function getb() below. any suggestion?
thks.
struct T_FILE_RAM {
ubyte[] buffer;
int offset;
void getb(void* buf, int size) {
buf=&b[offset..offset+size]; //<-- prob here
}
void read(out uint x) { getb(&x, x.sizeof); }
void read(out ushort x) { getb(&x, x.sizeof); }
}
Nov 05 2008
"James" wrote
im having problem in code below, while making my own file handler. the
data stored in buffer, so i can recall portion of data in diff format and
length like int,short,etc. but im stuck with function getb() below. any
suggestion? thks.
struct T_FILE_RAM {
ubyte[] buffer;
int offset;
void getb(void* buf, int size) {
buf=&b[offset..offset+size]; //<-- prob here
}
void read(out uint x) { getb(&x, x.sizeof); }
void read(out ushort x) { getb(&x, x.sizeof); }
}
I think you meant this?
buf[0..size] = buffer[offset..offset+size];
And also, you need to increment offset, no?
offset += size.
I'd also recommend using a template:
void getb(T)(ref T t)
{
auto size = t.sizeof;
auto buf = (cast(ubyte *)&t)[0..size];
buf[] = buffer[offset..offset+size];
offset += size;
}
Now you don't need to implement each type, and you can call like this:
int x;
ushort y;
getb(x); // reads 4 bytes
getb(y); // reads 2 bytes
-Steve
Nov 06 2008
On Thu, 06 Nov 2008 18:22:36 +0300, Steven Schveighoffer <schveiguy yahoo.com> wrote:"James" wroteGood solution also cares about endianess. Also take a look at tango.io.protocol - it should cover all your needs.im having problem in code below, while making my own file handler. the data stored in buffer, so i can recall portion of data in diff format and length like int,short,etc. but im stuck with function getb() below. any suggestion? thks. struct T_FILE_RAM { ubyte[] buffer; int offset; void getb(void* buf, int size) { buf=&b[offset..offset+size]; //<-- prob here } void read(out uint x) { getb(&x, x.sizeof); } void read(out ushort x) { getb(&x, x.sizeof); } }I think you meant this? buf[0..size] = buffer[offset..offset+size]; And also, you need to increment offset, no? offset += size. I'd also recommend using a template: void getb(T)(ref T t) { auto size = t.sizeof; auto buf = (cast(ubyte *)&t)[0..size]; buf[] = buffer[offset..offset+size]; offset += size; } Now you don't need to implement each type, and you can call like this: int x; ushort y; getb(x); // reads 4 bytes getb(y); // reads 2 bytes -Steve
Nov 06 2008
Steven Schveighoffer Wrote:"James" wrotethanks, exactly what im looking for. endianness isnt a problem at this point as im not working on a portable project.im having problem in code below, while making my own file handler. the data stored in buffer, so i can recall portion of data in diff format and length like int,short,etc. but im stuck with function getb() below. any suggestion? thks. struct T_FILE_RAM { ubyte[] buffer; int offset; void getb(void* buf, int size) { buf=&b[offset..offset+size]; //<-- prob here } void read(out uint x) { getb(&x, x.sizeof); } void read(out ushort x) { getb(&x, x.sizeof); } }I think you meant this? buf[0..size] = buffer[offset..offset+size]; And also, you need to increment offset, no? offset += size. I'd also recommend using a template: void getb(T)(ref T t) { auto size = t.sizeof; auto buf = (cast(ubyte *)&t)[0..size]; buf[] = buffer[offset..offset+size]; offset += size; } Now you don't need to implement each type, and you can call like this: int x; ushort y; getb(x); // reads 4 bytes getb(y); // reads 2 bytes -Steve
Nov 06 2008









"Denis Koroskin" <2korden gmail.com> 