data mapping, more elegant solution?

0ffh frank at frankhirsch.youknow.what.todo.net
Thu Dec 13 13:32:15 PST 2007


mandel wrote:
> Hi,
> 
> I want to map a uint to a position of an ubyte array.
> Atm. I use this:
> 
> ubyte[8] array;
> *cast(uint*) &array.ptr[0] = 42;
> 
> But is there a nicer solution to do achive this?
> 
> Smth. like  "array[0..4] = 42;", but this tries to assign 42 to every 
> byte (resulting in [0x2a, 0x2a, 0x2a, 0x2a, 0x00, 0x00, 0x00, 0x00]).

Well, if "array[0..4]=42;" does what you say, I think it does the right
thing. Your solution is the right one, altough I'd use more braces.
It's hard to be more elegant when crossing type boundaries. Some other
languages I could mention would not even allow the canonical C solution
(which is what you are doing)...

regards, frank


More information about the Digitalmars-d-learn mailing list