Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
November 30, 2007 getting the bytes of a long | ||||
---|---|---|---|---|
| ||||
hi, is there a way to stuff the 8 bytes of a long into a byte array other than to use a union to achive that? cheers jc |
November 30, 2007 Re: getting the bytes of a long | ||||
---|---|---|---|---|
| ||||
Posted in reply to jc | jc wrote:
> hi,
>
> is there a way to stuff the 8 bytes of a long into a byte array other than to use a union to achive that?
>
> cheers
> jc
Take the address, cast to byte, deref byte by byte:
(cast(ubyte*)&the_long)[0];
(cast(ubyte*)&the_long)[1];
(cast(ubyte*)&the_long)[2];
...
--bb
|
November 30, 2007 Re: getting the bytes of a long | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter wrote:
> jc wrote:
>> hi,
>>
>> is there a way to stuff the 8 bytes of a long into a byte array other than to use a union to achive that?
>>
>> cheers
>> jc
>
> Take the address, cast to byte, deref byte by byte:
>
> (cast(ubyte*)&the_long)[0];
> (cast(ubyte*)&the_long)[1];
> (cast(ubyte*)&the_long)[2];
> ...
Which means that you can go:
byte[] array = (cast(ubyte*)&the_long)[0..8];
if you want an actual array.
Regan
|
November 30, 2007 Re: getting the bytes of a long | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter wrote:
> jc wrote:
>> hi,
>>
>> is there a way to stuff the 8 bytes of a long into a byte array other than to use a union to achive that?
>>
>> cheers
>> jc
>
> Take the address, cast to byte, deref byte by byte:
>
> (cast(ubyte*)&the_long)[0];
> (cast(ubyte*)&the_long)[1];
> (cast(ubyte*)&the_long)[2];
> ...
>
> --bb
Does gdc run on any little endian machines? I'm not sure which way arrays grow on such machines, but I think this could would give the reverse byte order of a big endian machine.
|
November 30, 2007 Re: getting the bytes of a long | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jason House | Jason House wrote:
> Bill Baxter wrote:
>
>> jc wrote:
>>> hi,
>>>
>>> is there a way to stuff the 8 bytes of a long into a byte array other
>>> than to use a union to achive that?
>>>
>>> cheers
>>> jc
>> Take the address, cast to byte, deref byte by byte:
>>
>> (cast(ubyte*)&the_long)[0];
>> (cast(ubyte*)&the_long)[1];
>> (cast(ubyte*)&the_long)[2];
>> ...
>>
>> --bb
>
> Does gdc run on any little endian machines? I'm not sure which way arrays
> grow on such machines, but I think this could would give the reverse byte
> order of a big endian machine.
Yep. Order most definitely depends on endianness. Intel is little endian. If you're planning on sending those bytes over a network you'd better swap em if you've got version(LittleEndian).
--bb
|
December 07, 2007 Re: getting the bytes of a long | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jason House | "Jason House" <jason.james.house@gmail.com> wrote in message news:fipgc3$5ni$1@digitalmars.com... <snip> > Does gdc run on any little endian machines? I'm not sure which way arrays > grow on such machines, but I think this could would give the reverse byte > order of a big endian machine. Array indexes always follow the sign convention of memory addresses. The basic difference between big-endian and little-endian machines is which way the bytes of a number go relative to this sign convention. So yes. As such, accessing an integer as an array of bytes is a common technique for converting between little-endian and big-endian byte orders. Stewart. -- My e-mail address is valid but not my primary mailbox. Please keep replies on the 'group where everybody may benefit. |
Copyright © 1999-2021 by the D Language Foundation