May 17, 2012 Re: convert ubyte[k..k + 1] to int | ||||
---|---|---|---|---|
| ||||
Posted in reply to Artur Skawina | 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 are also aliasing
> issues, which i don't think are sufficiently defined for D (for
> C, it would be legal only because char* is allowed to alias anything).
>
> artur
Is it possible to ensure? In my case blob is created as
auto blob = cast(ubyte[]) read(fileName);
I assume that alignment is safe.
But what should I do to be safe in a general case?
|
May 21, 2012 Re: convert ubyte[k..k + 1] to int | ||||
---|---|---|---|---|
| ||||
Posted in reply to ref2401 | 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 bigEndianToNative, but you should check the endianes of the data that you desire convert to a int. For example: ubyte[5] array = createArray(); int value = littleEndianToNative!int(array); // or: // int value = bigEndianToNative!int(array); -- I'm afraid that I have a blog: http://zardoz.es |
Copyright © 1999-2021 by the D Language Foundation