data mapping, more elegant solution?

Regan Heath regan at netmail.co.nz
Thu Dec 13 06:33:31 PST 2007


mandel wrote:
> On Thu, 13 Dec 2007 13:35:35 +0000, Regan Heath wrote:
>> Use a union?
>>
>> union thing
>> {
>>    ubyte[8] ub;
>>    uint     ui;
>> }
>>
>> void main()
>> {
>>    thing a;
>>    a.ui = 42;
>> }
>>
> This way I can't insert data at arbitrary places, e.g. array[4..8].
> I also would have to cast thing to ubyte[8] when I pass it to functions.
> It's also hackish. :P

If you want to insert a 'short' into it then add an array of shorts too:

union thing
{
    ubyte[8]  ub;
    ushort[2] us;
    uint      ui;
}

thing a;

a.us[0] = 1; //ub[0..4]
a.us[1] = 2; //ub[4..8]

It's less hackish, and cleaner/clearer than using casts.

Regan


More information about the Digitalmars-d-learn mailing list