How to do "cast(ubyte[4])some_uint" in D1?

Nick Sabalausky a at a.a
Thu Jun 2 13:09:54 PDT 2011


"Timon Gehr" <timon.gehr at gmx.ch> wrote in message 
news:is7ojo$2ggv$1 at digitalmars.com...
>> Nick Sabalausky:
>>
>>> In D2, I can treat a uint as an array of ubytes by doing this:
>>>
>>> uint num = /+...whatever...+/;
>>> ubyte[] = cast(ubyte[4])num;
>>>
>>> How do I do that in D1?
>>
>> Using a union is probably the safest way:
>>
>> union Uint2Ubyte {
>>     uint u;
>>     ubyte[4] b;
>> }
>>
>> By the way, this of type conversions is a shady area in D.
>>
>> Bye,
>> bearophile
>
> I think it is no so shady after all:
> I tested the following code in DMD 2.053:
> void main(){
>    uint a;
>    ubyte[] b = cast(ubyte[4])a;
> }
>
> It gives the same error as in D1.
>

Ok, that's just bizarre. I've just check and verified that you're right. But 
I could swear I've done that sort of thing before, and without using the 
pointer trick Steve pointed out...





More information about the Digitalmars-d-learn mailing list