Concatenates int

Yota via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 10 10:01:34 PDT 2014


On Thursday, 10 July 2014 at 15:14:21 UTC, Sean Campbell wrote:
> 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.
>
> this may sound stupid (new to system programming) but how do 
> you convert to int form ubyte[]

int to ubyte[4]:
   auto iRCT = RawConvTypes!int();
   iRCT.value = 5;
   writeln(iRCT.bytes); // [5, 0, 0, 0]

ubyte[4] to int:
   auto iRCT = RawConvTypes!int();
   iRCT.bytes = [0, 1, 0, 0];
   writeln(iRCT.value); // 256


More information about the Digitalmars-d-learn mailing list