Casting byte[] to int/long

Bill Baxter wbaxter at gmail.com
Wed Nov 29 09:52:52 PST 2006


Henrik wrote:
> Hello!
> 
> If I have a dynamic array of bytes, say
> 
> byte[] a; a = [1,2,3,4];
> 
> Is it possible to somehow cast a number of these bytes into an int or a long,
> like so;
> 
> int b = cast(int)a[0..2];
> long c = cast(long)a;
> 
> or something? The goal is to convert 2 * 8 bit into 1 * 16 bit or 4 * 8 bit
> into 1 * 64 bit. 1) Is it possible? 2) Is it possible without confusing the
> GC? 3) How? ;)

You need something like:
   int b = *cast(int*)&a[0];
   long c = *cast(long*)a.ptr;

--bb



More information about the Digitalmars-d mailing list