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

Timon Gehr timon.gehr at gmx.ch
Thu Jun 2 03:28:08 PDT 2011


> 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.

Timon


More information about the Digitalmars-d-learn mailing list