Concatenates int

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 10 09:36:00 PDT 2014


On Thursday, 10 July 2014 at 13:51:22 UTC, Rikki Cattermole wrote:
> On 11/07/2014 1:18 a.m., Sean Campbell wrote:
>> perhaps I'd better state what I'm doing.
>> i have an array of 4 bytes and a want to convert them to a 32 
>> bit
>> int
>> and convert the 32 bit int back into a 4 bytes again.
>
> Small hack I use in Dakka:
>
> union RawConvTypes(T) {
> 	T value;
> 	ubyte[T.sizeof] bytes;
>
> 	ubyte[T.sizeof] opCast() {
> 		return bytes;
> 	}
> }
>
> auto iRCT = RawConvTypes!int(5);
> assert(iRCT.bytes == [5, 0, 0, 0]);
>
> Can be quite useful for evil conversions.

But as I understood the OP, he want's to use the bytes as decimal 
digits, i.e.

     assert(my_convert([4,7,0,1]) == 4701);

Reinterpret casting will not do this...


More information about the Digitalmars-d-learn mailing list