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

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


"Nick Sabalausky" <a at a.a> wrote in message 
news:is8qu8$1cq3$1 at digitalmars.com...
> "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...
>

Ah ha! I figured out what I had done before. In D2, casting a struct to a 
same-sized static array works fine:

struct Foo {uint f;}
void main(){
    Foo a;
    ubyte[] b = cast(ubyte[4])a;
}

Verified on DMD 2.053, 2.052 and 2.051. It doesn't work on D1 though (at 
least not 1.066 with tango trunk). But I'll try Steve's pointer trick for 
that.

As a nice touch, it fails in D2 if you change the "ubyte[4]" to "ubyte[5]". 
Using "ubyte[3]" is prohibited, too, even though I would think that should 
be ok. But that's probably not a real big deal.

But...it seems strange that casting a primitive to a static array would be 
prohibited if it's perfectly kosher when the primitive is in a struct. So 
I've filed a bug report on that:

Can't cast primitive to same-sized static array
http://d.puremagic.com/issues/show_bug.cgi?id=6092





More information about the Digitalmars-d-learn mailing list