View mode: basic / threaded / horizontal-split · Log in · Help
January 13, 2012
Absolute beginner
My first question si very silly:

string str = readln()

my input is for example 123

how can i convert this to an integer?

Thanks.
January 13, 2012
Re: Absolute beginner
Jorge wrote:
> My first question si very silly:
>
> string str = readln()
>
> my input is for example 123
>
> how can i convert this to an integer?

import std.conv;

// then in code:

auto i = to!int(str);
January 13, 2012
Re: Absolute beginner
Thanks for your answer but:

import std.stdio;
import std.conv;
void main()
{
	write("Insert number: ");
	string s = readln();
	auto i = to!int(s);
}

compiles but after i enter a number and press the enter key i get:

std.conv.ConvException@.\..\..\src\phobos\std\conv.d(1595): Can't
convert value
`
' of type string to type int
----------------
41DECC
41DD43
4027F3
402475
402D6C
402DB0
4029A7
4BBA79
----------------

what's wrong?
January 13, 2012
Re: Absolute beginner
Am 13.01.2012 22:16, Piotr Szturmaj wrote:
> Jorge wrote:
>> My first question si very silly:
>>
>> string str = readln()
>>
>> my input is for example 123
>>
>> how can i convert this to an integer?
>
> import std.conv;
>
> // then in code:
>
> auto i = to!int(str);

the string returned by readln() ends with NL ('\n'), which causes 
std.conv.to to raise an ConvException. You also must slice it away:

auto i = to!int(str[0..$-1]);

or you use std.conv.parse, which ignores the characters after the value 
and removes the converted characters from the string.
January 13, 2012
Re: Absolute beginner
Ok, it works fine. Thx to you all ;-) and sorry for my noob question.
January 13, 2012
Re: Absolute beginner
On 01/13/2012 10:34 PM, Jorge wrote:
> Thanks for your answer but:
>
> import std.stdio;
> import std.conv;
> void main()
> {
> 	write("Insert number: ");
> 	string s = readln();
> 	auto i = to!int(s);
> }
>
> compiles but after i enter a number and press the enter key i get:
>
> std.conv.ConvException@.\..\..\src\phobos\std\conv.d(1595): Can't
> convert value
> `
> ' of type string to type int
> ----------------
> 41DECC
> 41DD43
> 4027F3
> 402475
> 402D6C
> 402DB0
> 4029A7
> 4BBA79
> ----------------
>
> what's wrong?

readln() includes the trailing newline character in the resulting 
string. You can use std.string.strip to remove leading and trailing 
whitespace:

import std.stdio;
import std.conv;
import std.string;
void main()
{
    write("Insert number: ");
    string s = readln();
    auto i = to!int(strip(s));
}
January 13, 2012
Re: Absolute beginner
You could also try to!int(str.strip), strip() removes
whitespace from the left and right of a string.
You have to import std.string for it.

On 13 January 2012 22:34, Joshua Reusch <yoschi@arkandos.de> wrote:
> Am 13.01.2012 22:16, Piotr Szturmaj wrote:
>>
>> Jorge wrote:
>>>
>>> My first question si very silly:
>>>
>>> string str = readln()
>>>
>>> my input is for example 123
>>>
>>> how can i convert this to an integer?
>>
>>
>> import std.conv;
>>
>> // then in code:
>>
>> auto i = to!int(str);
>
>
> the string returned by readln() ends with NL ('\n'), which causes
> std.conv.to to raise an ConvException. You also must slice it away:
>
> auto i = to!int(str[0..$-1]);
>
> or you use std.conv.parse, which ignores the characters after the value and
> removes the converted characters from the string.
January 13, 2012
Re: Absolute beginner
Timon Gehr:

> readln() includes the trailing newline character in the resulting 
> string. You can use std.string.strip to remove leading and trailing 
> whitespace:

Time ago I have asked Andrei to modify the to!int conversion to work as Python, ignoring leading and trailing whitespace:

>>> s = "123\n"
>>> int(s)
123

But he didn't change it. I don't think people use parse at their first try.

Bye,
bearophile
January 13, 2012
Re: Absolute beginner
While we're at it: what's the best way to parse in a formatted manner?
For example, if I want to get 5 hexadecimal digits converted into an
uint? And I want to simultaneously advance the string?

"sscanf" seems fiddly and unsafe.

On 13 January 2012 22:56, bearophile <bearophileHUGS@lycos.com> wrote:
> Timon Gehr:
>
>> readln() includes the trailing newline character in the resulting
>> string. You can use std.string.strip to remove leading and trailing
>> whitespace:
>
> Time ago I have asked Andrei to modify the to!int conversion to work as Python, ignoring leading and trailing whitespace:
>
>>>> s = "123\n"
>>>> int(s)
> 123
>
> But he didn't change it. I don't think people use parse at their first try.
>
> Bye,
> bearophile
January 14, 2012
Re: Absolute beginner
On Fri, Jan 13, 2012 at 11:05:19PM +0100, Matej Nanut wrote:
> While we're at it: what's the best way to parse in a formatted manner?
> For example, if I want to get 5 hexadecimal digits converted into an
> uint?
[...]

Have you tried:

	import std.conv;
	...
	uint val = parse!uint(input_string[0..5], 16);

?


> And I want to simultaneously advance the string?

You can always keep track of how many characters were consumed by
parse() and advancing the string yourself. Something like this:

	import std.conv;
	string tmp = input_string[0..5];
	uint val = parse!uint(tmp, 16);
	input_string = input_string[5-tmp.length .. $];

If you find this a bit verbose, you can always encapsulate it in a
function and use that instead.


> "sscanf" seems fiddly and unsafe.

IMNSHO, sscanf is one of the poorest designed functions we had the
misfortune to inherit from the C library. It pretends to mirror printf()
in its format string, but the semantics are subtly different and likes
to come back to bite you unexpectedly at the most inopportune times (if
you ever tried using "%s" to read back a string printed using "%s", or
tried to count embedded spaces in the input, you'll know what I mean).
In C and C++ (can't speak for the D wrappers for it) it also suffers
from potential buffer overruns, pointer bugs, and all sorts of other
lovely things. It seems so nice and simple to use, but comes with all
sorts of nasty gotcha's, bad corner cases, etc..

I say, avoid sscanf like the plague. There are many other much better
alternatives.


T

-- 
Klein bottle for rent ... inquire within. -- Stephen Mulraney
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home