Thread overview
Conversion string->int
Jun 07, 2014
Paul
Jun 07, 2014
monarch_dodra
Jun 07, 2014
Paul
Jun 07, 2014
Jonathan M Davis
Jun 07, 2014
anonymous
Jun 08, 2014
Jonathan M Davis
Jun 08, 2014
Paul
June 07, 2014
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?
June 07, 2014
On Saturday, 7 June 2014 at 20:53:03 UTC, Paul 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?

parse takes an lvalue to a range. char[2] is a static array, and is not a range. You need to store an actual "char[]" (or string) in a variable to call parse. So "ss" will work, "to!string(s)" will not.
June 07, 2014
On Saturday, 7 June 2014 at 20:53:03 UTC, Paul 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?

`parse` takes its argument by ref. It pops the characters it
reads from the source. So in your first snippet, ss will be empty
when parse is done with it.

> And what is the right way to parse char[2]->int with radix?

In this case you can use to!uint(foo, 16) instead.
June 07, 2014
On Saturday, 7 June 2014 at 21:15:37 UTC, monarch_dodra wrote:
> On Saturday, 7 June 2014 at 20:53:03 UTC, Paul 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?
>
> parse takes an lvalue to a range. char[2] is a static array, and is not a range. You need to store an actual "char[]" (or string) in a variable to call parse. So "ss" will work, "to!string(s)" will not.

But to!string(s) creates temporary string object or I am not right? And this temp string is pushed as argument of parse... Or not?
June 07, 2014
On Sat, 07 Jun 2014 21:22:03 +0000
Paul via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:

> On Saturday, 7 June 2014 at 21:15:37 UTC, monarch_dodra wrote:
> > On Saturday, 7 June 2014 at 20:53:03 UTC, Paul 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?
> >
> > parse takes an lvalue to a range. char[2] is a static array,
> > and is not a range. You need to store an actual "char[]" (or
> > string) in a variable to call parse. So "ss" will work,
> > "to!string(s)" will not.
>
> But to!string(s) creates temporary string object or I am not right? And this temp string is pushed as argument of parse... Or not?

to!string will allocate a string if it's passed anything other than string. However, parse requires an lvalue, and to!string returns an rvlaue, so passing the result of to!string directly to parse isn't going to work. You have to assign it to a variable first.

- Jonathan M Davis
June 08, 2014
On Sat, 07 Jun 2014 20:53:02 +0000
Paul via Digitalmars-d-learn <digitalmars-d-learn@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
June 08, 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@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!!