digitalmars.D.learn - cast
- Boris Shomodjvarac (8/8) Nov 17 2006 Hi,
- Max Samuha (5/13) Nov 17 2006 *cast(int*)&buffer[4]
- Boris Shomodjvarac (2/25) Nov 17 2006 Thanks it works :) I also want to thank Bill and Gordon...
- Bill Baxter (5/20) Nov 17 2006 *cast(int*)&buffer[4];
- Stewart Gordon (15/29) Nov 17 2006 An array cannot be converted to an int. An array of bytes can, however,...
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
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
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:Thanks it works :) I also want to thank Bill and Gordon...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
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
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