Integer to hexadecimal string

Jarrett Billingsley kb3ctd2 at yahoo.com
Wed Jul 9 11:17:44 PDT 2008


"BCS" <ao at pathlink.com> wrote in message 
news:55391cb32ef558caafbcbd0e7af2 at news.digitalmars.com...
> Reply to Jarrett,
>
>> "BCS" <ao at pathlink.com> wrote in message
>> news:55391cb32eed78caaf3194222520 at news.digitalmars.com...
>>
>>> you can even force the format string to be built at compile time.
>>>
>>> char[] toHex(T)(T arg){ return format("%."~itoa!(T.sizeof * 2)~"x",
>>> arg); }
>>>
>> No function necessary.
>>
>> return format("%." ~ (T.sizeof * 2).stringof ~ "x", arg);
>>
>> Untested, but you can use .stringof to do number to string at compile
>> time. Not sure if it'll give "T.sizeof * 2" instead here :P  might
>> have to put it in a const.
>>
>
> I think that won't work. The only place I know that .stringof works that 
> way is __LINE__.stringof but that's special cased in the lexer IIRC.

Just try it, please?

template Itoa(int x)
{
    const Itoa = x.stringof;
}

pragma(msg, (5).stringof); // "5"
pragma(msg, (3 + 4).stringof); // "3 + 4"
pragma(msg, Itoa!(3 + 4)); // "7"




More information about the Digitalmars-d-learn mailing list