www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - cast

reply Boris Shomodjvarac <shomodj.N SPAM.slashmail.org> writes:
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.
Nov 17 2006
next sibling parent reply Max Samuha <maxter i.com.ua> writes:
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)
Nov 17 2006
parent Boris Shomodjvarac <shomodj.N SPAM.slashmail.org> writes:
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...
Nov 17 2006
prev sibling next sibling parent Bill Baxter <wbaxter gmail.com> writes:
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
Nov 17 2006
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
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.
Nov 17 2006