Re: Converting a ulong to a byte array and constructing a ulong from it

2019-10-24 Thread welkam via Digitalmars-d-learn
On Thursday, 24 October 2019 at 14:08:36 UTC, 9898287 wrote: Does this contain any undefined behavior? It is in C as far as I knew. immutable int number = 1; auto bad_idea = (cast(ubyte*) &number)[0 .. number.sizeof]; bad_idea[0] = 2; writeln(number); //1 writeln(*(cast(int*)bad_idea.ptr)); //2

Re: Converting a ulong to a byte array and constructing a ulong from it

2019-10-24 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 24 October 2019 at 14:08:36 UTC, 9898287 wrote: On Thursday, 24 October 2019 at 13:50:54 UTC, Paul Backus wrote: Use a cast: ulong m = *cast(ulong*) bytes.ptr; Does this contain any undefined behavior? It is in C as far as I knew. The equivalent code in C would use a char*

Re: Converting a ulong to a byte array and constructing a ulong from it

2019-10-24 Thread Daniel Kozak via Digitalmars-d-learn
On Thu, Oct 24, 2019 at 3:35 PM 9898287 via Digitalmars-d-learn wrote: > > What's the function for converting a ulong to a native-endian > byte array? > For example, > > auto bytes = 0x1234567890123456u64.to_ne_bytes(); > // should yield > // [0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56] in big

Re: Converting a ulong to a byte array and constructing a ulong from it

2019-10-24 Thread Radu via Digitalmars-d-learn
On Thursday, 24 October 2019 at 13:33:30 UTC, 9898287 wrote: What's the function for converting a ulong to a native-endian byte array? For example, auto bytes = 0x1234567890123456u64.to_ne_bytes(); // should yield // [0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56] in big-endian and // [0x56,

Re: Converting a ulong to a byte array and constructing a ulong from it

2019-10-24 Thread 9898287 via Digitalmars-d-learn
On Thursday, 24 October 2019 at 13:50:54 UTC, Paul Backus wrote: On Thursday, 24 October 2019 at 13:33:30 UTC, 9898287 wrote: What's the function for converting a ulong to a native-endian byte array? For example, auto bytes = 0x1234567890123456u64.to_ne_bytes(); // should yield // [0x12, 0x34,

Re: Converting a ulong to a byte array and constructing a ulong from it

2019-10-24 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 24 October 2019 at 13:33:30 UTC, 9898287 wrote: What's the function for converting a ulong to a native-endian byte array? For example, auto bytes = 0x1234567890123456u64.to_ne_bytes(); // should yield // [0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56] in big-endian and // [0x56,

Converting a ulong to a byte array and constructing a ulong from it

2019-10-24 Thread 9898287 via Digitalmars-d-learn
What's the function for converting a ulong to a native-endian byte array? For example, auto bytes = 0x1234567890123456u64.to_ne_bytes(); // should yield // [0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56] in big-endian and // [0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12] in little-endian sy