Binary data in the code?

Bill Baxter wbaxter at gmail.com
Sun Nov 5 11:56:09 PST 2006


Chris Nicholson-Sauls wrote:
> 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

No dice:
   static const auto data = x"00 10 A3"c;
   --> ?Error: 4invalid UTF-8 sequence

The ? is actually some non-ascii character.

--bb



More information about the Digitalmars-d-learn mailing list