Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
November 17, 2006 cast | ||||
---|---|---|---|---|
| ||||
Hi, I have a array of ubyte-s and need to cast for example from 4-th element to 8-th element into int, anyone knows how to do that? example: ubyte buffer[1024]; listener.receiveFrom(buffer, address); writefln("Transaction ID: %s", cast(int)buffer[4 .. 8]); Thanks. |
November 17, 2006 Re: cast | ||||
---|---|---|---|---|
| ||||
Posted in reply to Boris Shomodjvarac | On Fri, 17 Nov 2006 14:01:50 +0100, Boris Shomodjvarac <shomodj.N@SPAM.slashmail.org> wrote:
>
>Hi,
>
>I have a array of ubyte-s and need to cast for example from 4-th element to 8-th element into int, anyone knows how to do that?
>
>example:
>
>ubyte buffer[1024];
>
>listener.receiveFrom(buffer, address);
>
>writefln("Transaction ID: %s", cast(int)buffer[4 .. 8]);
>
>
>Thanks.
*cast(int*)&buffer[4]
or
*cast(int*)(buffer.ptr + 4)
|
November 17, 2006 Re: cast | ||||
---|---|---|---|---|
| ||||
Posted in reply to Boris Shomodjvarac | Boris Shomodjvarac wrote:
> Hi,
>
> I have a array of ubyte-s and need to cast for example from 4-th element
> to 8-th element into int, anyone knows how to do that?
>
> example:
>
> ubyte buffer[1024];
>
> listener.receiveFrom(buffer, address);
>
> writefln("Transaction ID: %s", cast(int)buffer[4 .. 8]);
>
>
> Thanks.
*cast(int*)&buffer[4];
But you better be sure the endianness of the data is the same as the platform you're reading it on.
--bb
|
November 17, 2006 Re: cast | ||||
---|---|---|---|---|
| ||||
Posted in reply to Boris Shomodjvarac | Boris Shomodjvarac wrote: > Hi, > > I have a array of ubyte-s and need to cast for example from 4-th element > to 8-th element into int, anyone knows how to do that? > > example: > > ubyte buffer[1024]; > > listener.receiveFrom(buffer, address); > > writefln("Transaction ID: %s", cast(int)buffer[4 .. 8]); > > Thanks. An array cannot be converted to an int. An array of bytes can, however, be converted to an array of ints. (cast(int[]) cast(void[]) buffer[4..8]) [0] If your data needs to be portable between big-endian and little-endian platforms, you'll need to be a bit more careful. Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:-@ C++@ a->--- UB@ P+ L E@ W++@ N+++ o K-@ w++@ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit. |
November 17, 2006 Re: cast | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samuha | On Fri, 17 Nov 2006 15:30:53 +0200, Max Samuha wrote:
> On Fri, 17 Nov 2006 14:01:50 +0100, Boris Shomodjvarac <shomodj.N@SPAM.slashmail.org> wrote:
>
>>
>>Hi,
>>
>>I have a array of ubyte-s and need to cast for example from 4-th element to 8-th element into int, anyone knows how to do that?
>>
>>example:
>>
>>ubyte buffer[1024];
>>
>>listener.receiveFrom(buffer, address);
>>
>>writefln("Transaction ID: %s", cast(int)buffer[4 .. 8]);
>>
>>
>>Thanks.
>
> *cast(int*)&buffer[4]
> or
> *cast(int*)(buffer.ptr + 4)
Thanks it works :) I also want to thank Bill and Gordon...
|
Copyright © 1999-2021 by the D Language Foundation