Integer to hexadecimal string

BCS ao at pathlink.com
Tue Jul 8 16:48:27 PDT 2008


Reply to Dave,

> jicman wrote:
> 
>> novice2 Wrote:
>> 
>>> jicman Wrote:
>>> 
>>>> other files.  This file which this COM object uses as input I
>>>> create using D.  This file is created by my doing a
>>>> std.file.listdir(fd,"*") and then using
>>>> 
>>> imho, you should use std.windows.charset.toMBSz() while you pass
>>> string from D to Windows API, and std.windows.charset.fromMBSz()
>>> while you pass string back from Windows API to D
>>> 
>> Is this part of Phobos 1.0?  I only see in in Phobos 2.0. Is there a
>> similar call within Phobos 1.0?
>> 
> why not use format??
> char[] hex = format("%.8x", 12345);
> that produces hex..
> 
> import std.string;
> char[] toHex(T)(T arg){
> return format("%."~std.string.toString(T.sizeof * 2)~"x", arg);
> }
> i haven't tested that code. but i think it'd work for ulong long uint
> int ubyte and byte.
> 
> -dave
> 

If you steal some code from Don Clugston:
http://www.dsource.org/projects/ddl/browser/trunk/meta/conv.d

template decimaldigit(int n) { const char [] decimaldigit = "0123456789"[n..n+1]; 
}
template itoa(long n)
{
static if (n<0) const char [] itoa = "-" ~ itoa!(-n); 
else static if (n<10L) const char [] itoa = decimaldigit!(n);
else const char [] itoa = itoa!(n/10L) ~ decimaldigit!(n%10L);
}

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); }




More information about the Digitalmars-d-learn mailing list