Binary data in the code?

Bill Baxter wbaxter at gmail.com
Sat Nov 4 10:51:31 PST 2006


Chris Nicholson-Sauls wrote:
> Jarrett Billingsley wrote:
> 
>> "Bill Baxter" <wbaxter at gmail.com> wrote in message 
>> news:eihhpl$1ish$1 at digitaldaemon.com...
>>
>>
>>> Or live with a dynamic array:
>>>  static const ubyte[] data = {99,12,2,21};
>>
>>
>>
>> Actually, if you then write
>>
>> data.length = 6;
>>
>> The compiler will complain.

The compilers complaints can be useful for the fixed-length array --
   static const ubyte[1 /*intentionally wrong*/] data = [99,12,2,21];

The compiler will complain 'hey 1 is to small for 4 elements'.
Really annoying to have to try to compile once to get the fixed length 
for the array, but it works.

It seems like there should be a syntax for a fixed-length array with 
automatically-deduced length.

I suggest either

   static const ubyte[$] data = [99,12,2,21];

taking the '$'-means-length syntax from arrays.  That would suggest that 
ubyte[length] data should also work.

Or alternatively use the 'auto' keyword:

   static const ubyte[auto] data = [99,12,2,21];


---
Anyway, I do find it useful to embed binary data in the app quite often, 
so I hope someone can chime in with the right way to do it in D.  It's 
good for inlining little images and things like that.
In fact, I have a little command line utility program for C++ that I use 
just for that purpose.  It takes binary files and converts them to C++ 
source files. (It's a slightly modified version of this: 
http://fox-toolkit.net/cgi-bin/wiki.pl?Tutorial_14_Reswrap)

I guess if I wrote a D version of that tool then I could have it 
automatically spit out the length as well, assuming fixed-length arrays 
are the right way to do this.

--bb



More information about the Digitalmars-d-learn mailing list