Re: convert ubyte[k..k + 1] to int

2012-05-21 Thread Luis Panadero GuardeƱo
ref2401 wrote: i have an array of ubytes. how can i convert two adjacent ubytes from the array to an integer? pseudocode example: ubyte[5] array = createArray(); int value = array[2..3]; is there any 'memcpy' method or something else to do this? Try to use littleEndianToNative or

Re: convert ubyte[k..k + 1] to int

2012-05-17 Thread Roman D. Boiko
On Thursday, 17 May 2012 at 04:16:10 UTC, Andrew Wiley wrote: On Wed, May 16, 2012 at 11:07 PM, H. S. Teoh hst...@quickfur.ath.cx wrote: Do unions suffer from this problem? Could this prevent alignment problems: short bytesToShort(ubyte[] b) in { assert(b.length==2); }

Re: convert ubyte[k..k + 1] to int

2012-05-17 Thread Roman D. Boiko
On Thursday, 17 May 2012 at 07:07:58 UTC, Roman D. Boiko wrote: And what about the following code: // This implementation is optimized for speed via swapping endianness in-place pure immutable(C)[] fixEndian(C, Endian blobEndian = endian)(ubyte[] blob) if(is(CharTypeOf!C)) { import

Re: convert ubyte[k..k + 1] to int

2012-05-17 Thread Artur Skawina
On 05/17/12 10:15, Roman D. Boiko wrote: On Thursday, 17 May 2012 at 07:07:58 UTC, Roman D. Boiko wrote: And what about the following code: // This implementation is optimized for speed via swapping endianness in-place pure immutable(C)[] fixEndian(C, Endian blobEndian = endian)(ubyte[]

Re: convert ubyte[k..k + 1] to int

2012-05-17 Thread Roman D. Boiko
On Thursday, 17 May 2012 at 08:39:21 UTC, Artur Skawina wrote: On 05/17/12 10:15, Roman D. Boiko wrote: I mean, is it safe (assuming that we are allowed to mutate blob, and its length is a multiple of C.sizeof)? I do casting from ubyte[] to C[]. Only if C.ptr ends up properly aligned. There

Re: convert ubyte[k..k + 1] to int

2012-05-17 Thread Artur Skawina
On 05/17/12 10:47, Roman D. Boiko wrote: On Thursday, 17 May 2012 at 08:39:21 UTC, Artur Skawina wrote: On 05/17/12 10:15, Roman D. Boiko wrote: I mean, is it safe (assuming that we are allowed to mutate blob, and its length is a multiple of C.sizeof)? I do casting from ubyte[] to C[].

Re: convert ubyte[k..k + 1] to int

2012-05-17 Thread Artur Skawina
On 05/17/12 11:36, Artur Skawina wrote: assert(cast(size_t)data.ptr%data.alignof==0); This assert can be easily triggered eg by doing blob[1..$-4]; blob.assumeSafeAppend(); ++blob.length; auto data = cast(C[]) blob; Umm, that was meant to be: blob = blob[1..$-4];

convert ubyte[k..k + 1] to int

2012-05-16 Thread ref2401
i have an array of ubytes. how can i convert two adjacent ubytes from the array to an integer? pseudocode example: ubyte[5] array = createArray(); int value = array[2..3]; is there any 'memcpy' method or something else to do this?

Re: convert ubyte[k..k + 1] to int

2012-05-16 Thread Regan Heath
On Wed, 16 May 2012 15:24:33 +0100, ref2401 refacto...@gmail.com wrote: i have an array of ubytes. how can i convert two adjacent ubytes from the array to an integer? pseudocode example: ubyte[5] array = createArray(); int value = array[2..3]; is there any 'memcpy' method or something else

Re: convert ubyte[k..k + 1] to int

2012-05-16 Thread Jonathan M Davis
On Wednesday, May 16, 2012 17:03:44 Regan Heath wrote: On Wed, 16 May 2012 15:24:33 +0100, ref2401 refacto...@gmail.com wrote: i have an array of ubytes. how can i convert two adjacent ubytes from the array to an integer? pseudocode example: ubyte[5] array = createArray(); int value

Re: convert ubyte[k..k + 1] to int

2012-05-16 Thread Robert DaSilva
On Wednesday, 16 May 2012 at 18:47:55 UTC, Jonathan M Davis wrote: As long as you're going from big endian to little endian, std.bitmanip.bigEndianToNative will do the conversion fairly easily, but if they're in little endian, then the nasty casting is the way to go. - Jonathan M Davis

Re: convert ubyte[k..k + 1] to int

2012-05-16 Thread Jonathan M Davis
On Thursday, May 17, 2012 02:32:55 Robert DaSilva wrote: On Wednesday, 16 May 2012 at 18:47:55 UTC, Jonathan M Davis wrote: As long as you're going from big endian to little endian, std.bitmanip.bigEndianToNative will do the conversion fairly easily, but if they're in little endian, then

Re: convert ubyte[k..k + 1] to int

2012-05-16 Thread Andrew Wiley
On Wed, May 16, 2012 at 11:03 AM, Regan Heath re...@netmail.co.nz wrote: On Wed, 16 May 2012 15:24:33 +0100, ref2401 refacto...@gmail.com wrote: i have an array of ubytes. how can i convert two adjacent ubytes from the array to an integer? pseudocode example: ubyte[5] array =

Re: convert ubyte[k..k + 1] to int

2012-05-16 Thread H. S. Teoh
On Wed, May 16, 2012 at 10:25:51PM -0500, Andrew Wiley wrote: On Wed, May 16, 2012 at 11:03 AM, Regan Heath re...@netmail.co.nz wrote: On Wed, 16 May 2012 15:24:33 +0100, ref2401 refacto...@gmail.com wrote: i have an array of ubytes. how can i convert two adjacent ubytes from the array

Re: convert ubyte[k..k + 1] to int

2012-05-16 Thread Andrew Wiley
On Wed, May 16, 2012 at 11:07 PM, H. S. Teoh hst...@quickfur.ath.cx wrote: On Wed, May 16, 2012 at 10:25:51PM -0500, Andrew Wiley wrote: On Wed, May 16, 2012 at 11:03 AM, Regan Heath re...@netmail.co.nz wrote: On Wed, 16 May 2012 15:24:33 +0100, ref2401 refacto...@gmail.com wrote: