struct to byte[]

Kirk McDonald kirklin.mcdonald at gmail.com
Tue Dec 12 12:33:40 PST 2006


Luís Marques wrote:
> Hello,
> 
> Converting a structure to ubyte[] or similar is something that I have 
> been doing frequently while converting C to D code.
> 
> Can we have a "cast(type[])" working for structures, please?
> 
> I suppose that type should include at least ubyte, byte and char, if not 
> all the basic data types.
> 
> Is there a reason for this explicit cast not to work automatically? (I 
> know that templates can alleviate this)
> 
> E.g.
> 
> struct Foo
> {
>     int a;
>     float b;
> }
> 
> converts to ubyte[8] via "cast(ubyte[])".
> 
> 
> -- 
> Luís

Foo f;
ubyte[] u = (cast(ubyte*)&f)[0 .. Foo.sizeof];

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://pyd.dsource.org



More information about the Digitalmars-d mailing list