Binary data in the code?

Chris Nicholson-Sauls ibisbasenji at gmail.com
Sun Nov 5 09:50:49 PST 2006


Bill Baxter wrote:
> BCS wrote:
> 
>> == Quote from Bill Baxter (wbaxter at gmail.com)'s article
>>
>>> 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.
>>
>>
>> [...]
>>
>>> Thanks,
>>> --bb
>>
>>
>>
>>
>> IIRC, this works:
>>
>> auto data = [cast(char)99,12,2,21];
>> // type of data == char[4];
> 
> 
> 
> Ick.  Combines the lack of readability of 'auto' with the lameness of 
> D's first-value array typing rule.
> 
> It's beside the point though, since that doesn't actually work.
>    staticdata.d(9): Error: cannot infer type from initializer
> 
> --bb

Well, if as in the example one wishes to use char[] as the holding data type, one can 
always just use D's HexStrings.  Then the example becomes:

const data = x"63 0C 02 15"c ;

-- Chris Nicholson-Sauls



More information about the Digitalmars-d-learn mailing list