FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Assigning a scalar to an array...

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
seanc



Joined: 21 Jul 2007
Posts: 2

PostPosted: Sun Jul 22, 2007 12:05 am    Post subject: Assigning a scalar to an array... Reply with quote

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
View user's profile Send private message
seanc



Joined: 21 Jul 2007
Posts: 2

PostPosted: Sun Jul 22, 2007 8:45 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group