read Hexadecimal value from string

David L. Davis SpottedTiger at yahoo.com
Tue Jul 17 14:03:51 PDT 2007


Frank Benoit Wrote:

> When using Tango, this can be done like this:
> 
> import tango.text.convert.Integer;
> int  i = toInt( "12AB", 16 );
> long l = toLong( "ffffFFFFAAAA", 16 );
> 
> see also:
> http://www.dsource.org/projects/tango/wiki/ChapterConversions
> and the source at
> http://www.dsource.org/projects/tango/browser/trunk/tango/text/convert/Integer.d
> 
> Frank

Yep, Tango has some very kool functions. But I do want to point out that Phobos does have a radix parameter in the toString functions for converting a number to a string...sadly not the other way around.

Here's an example:
// Using Phobos you can convert a long or a ulong value, 
// using any base from 2 to 36 into a string
private import ss = std.string;
private import io = std.stdio;

void main()
{
    auto s = ss.toString(12uL, 16u);
    io.writefln("ss.toString(12uL, 16u)=\"%s\"", s);
}

/+ Output:
C:\dmd>dmd strex1.d
C:\dmd\bin\..\..\dm\bin\link.exe strex1,,,user32+kernel32/noi;

C:\dmd>strex1
ss.toString(12uL, 16u)="C"

C:\dmd>
+/

David L. Davis


More information about the Digitalmars-d-learn mailing list