View previous topic :: View next topic |
Author |
Message |
seanc
Joined: 21 Jul 2007 Posts: 2
|
Posted: Sun Jul 22, 2007 12:05 am Post subject: Assigning a scalar to an array... |
|
|
Howdy. This should be simple and I know I must be missing something. I'd like to have the following snippet of code work:
Code: | ubyte[12] buf;
uint32_t a = 42;
uint64_t b = 42424242424242;
buf[0..3] = a;
buf[4..11] = b; |
I think I've tried every combo of casting I know of to get that to work, but can't. I know there is an issue with Endianness but.... am I missing an obvious way of accomplishing this? |
|
Back to top |
|
|
seanc
Joined: 21 Jul 2007 Posts: 2
|
Posted: Sun Jul 22, 2007 8:45 am Post subject: |
|
|
Alright, looks like I've got a winner. Thanks!
Code: | import std.stdio;
import std.stdint;
int main() {
ubyte[12] buf;
uint32_t a;
uint64_t b;
size_t off = 0;
a = 32;
b = 424242424242;
*cast(typeof(a)*)&buf[off] = a;
off += a.sizeof;
*cast(typeof(b)*)&buf[off] = b;
off += b.sizeof;
writefln("%d %d", *cast(typeof(a)*)&buf[0], *cast(typeof(b)*)&buf[4]);
return 0;
} |
Thanks to Mandel on #d! |
|
Back to top |
|
|
|