getting the bytes of a long

Regan Heath regan at netmail.co.nz
Fri Nov 30 00:57:34 PST 2007


Bill Baxter wrote:
> jc wrote:
>> hi,
>>
>> is there a way to stuff the 8 bytes of a long into a byte array other 
>> than to use a union to achive that?
>>
>> cheers
>> jc
> 
> Take the address, cast to byte, deref byte by byte:
> 
>     (cast(ubyte*)&the_long)[0];
>     (cast(ubyte*)&the_long)[1];
>     (cast(ubyte*)&the_long)[2];
>     ...

Which means that you can go:

byte[] array = (cast(ubyte*)&the_long)[0..8];

if you want an actual array.

Regan


More information about the Digitalmars-d-learn mailing list