Binary data in the code?

Bill Baxter wbaxter at gmail.com
Sat Nov 4 00:08:19 PST 2006


How does one embed binary data in code with D?

In C/C++ one would do something like:

   static const unsigned char data[] = {99,12,2,21};

The compiler counts the elements for you, but you still get a 
compile-time length, so you can do sizeof(data) and you'll get back 4 in 
the above example.

With D it looks like to get a fixed-size array I have to count the 
elements myself (annoying if there are a page full of numbers):
   static const ubyte[4] data = {99,12,2,21};

Or live with a dynamic array:
   static const ubyte[] data = {99,12,2,21};

Neither of which seems to be quite what I want, which is to end up with 
exactly one non-modifiable copy of the data in the data segment.

Thanks,
--bb



More information about the Digitalmars-d-learn mailing list