4-character literal

Bill Baxter dnewsgroup at billbaxter.com
Thu Jan 25 23:44:38 PST 2007


Bill Baxter wrote:
> Rick Mann wrote:
>> Rick Mann Wrote:
>>
>>> enum : uint
>>> {
>>>     kSomeConstantValue = 'abcd'
>>> }
>>
>>
>> I realized I was misunderstanding something else I saw, and that 
>> "abcd"d doesn't do what I thought (make a 4-byte character).
>>
>> So: how to I do the equivalent of 'abcd'?
>>
>> Thanks!
> 
> Interesting question.  How's this?
> 
> import std.stdio;
> 
> template touint(char[] T)
> {
>     static assert(T.length==4, "Integer constants must be of length 4");
>     const uint touint =
>         (cast(char)T[0] << 24)|
>         (cast(char)T[1] << 16)|
>         (cast(char)T[2] << 8)|
>         (cast(char)T[3]);
> }
> 
> enum
> {
>     kSomeConstantValue = touint!("xyzz")
> }
> 
> void main()
> {
>     writefln("%x", kSomeConstantValue);
> }
> 

Damn!  Torhu beat me too it!

--bb



More information about the Digitalmars-d mailing list