Conversion string->int
Paul via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jun 7 22:23:05 PDT 2014
On Sunday, 8 June 2014 at 01:44:27 UTC, Jonathan M Davis via
Digitalmars-d-learn wrote:
> On Sat, 07 Jun 2014 20:53:02 +0000
> Paul via Digitalmars-d-learn
> <digitalmars-d-learn at puremagic.com> wrote:
>
>> I can not understand, why this code works:
>>
>> char s[2] = ['0', 'A'];
>> string ss = to!string(s);
>> writeln(parse!uint(ss, 16));
>>
>> but this can deduces template:
>>
>> char s[2] = ['0', 'A'];
>> writeln(parse!uint(to!string(s), 16));
>>
>> What's the reason? And what is the right way to parse
>> char[2]->int with radix?
>
> std.conv.to converts the entire string at once. std.conv.parse
> takes the
> beginning of the string until it finds whitespace, and converts
> that first
> part of the string. And because it does that, it takes the
> string by ref so
> that it's able to actually pop the elements that it's
> converting off of the
> front of the string, leaving the rest of the string behind to
> potentially be
> parsed as something else, whereas because std.conv.to converts
> the whole
> string, it doesn't need to take its argument by ref.
>
> So, what's causing you trouble you up is the ref, because if a
> parameter is a
> ref parameter, then it only accepts lvalues, so you have to
> pass it an actual
> variable, not the result of to!string. Also,
>
> string s = ['A', '0'];
>
> will compile, so you don't need to use to!string in this case.
>
> - Jonathan M Davis
Tnank you very much!!
More information about the Digitalmars-d-learn
mailing list