to(T, ubyte base) if (isIntegral!T)

Shriramana Sharma via Digitalmars-d digitalmars-d at puremagic.com
Sat Oct 17 08:31:48 PDT 2015


Marc Schütz wrote:

> void main() {
> import std.conv;
> assert("1f".to!int(16) == 31);
> }

Hey thanks. In case the library maintainer wants to tighten up the unittest 
for this, I wrote up a short Python to produce a unittest for this, since 
Python also has the same support.

#! /usr/bin/env python3
from random import choice
digitsH = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
digitsL = digitsH.lower()
digits = (digitsH, digitsL)
print("""\
import std.conv;
unittest
{\
""")
for base in range(2, 37):
    for i in range(8):
        s = "".join(choice(choice(digits)[:base]) for j in range(8))
        v = int(s, base)
        print('    assert("{}".to!ulong({:>2}) == {:>20});'.format(s, base, 
v))
print("}")

-- 
Shriramana Sharma, Penguin #395953


More information about the Digitalmars-d mailing list