www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Absolute beginner

reply Jorge <Jorge991733aplha gmail.com> writes:
My first question si very silly:

string str = readln()

my input is for example 123

how can i convert this to an integer?

Thanks.
Jan 13 2012
parent reply Piotr Szturmaj <bncrbme jadamspam.pl> writes:
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);
Jan 13 2012
next sibling parent reply Jorge <jorge991733alpha gmail.com> writes:
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?
Jan 13 2012
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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)); }
Jan 13 2012
parent bearophile <bearophileHUGS lycos.com> writes:
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)



But he didn't change it. I don't think people use parse at their first try. Bye, bearophile
Jan 13 2012
prev sibling next sibling parent reply Joshua Reusch <yoschi arkandos.de> writes:
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.
Jan 13 2012
parent Jorge <jorge991733alpha gmail.com> writes:
Ok, it works fine. Thx to you all ;-) and sorry for my noob question.
Jan 13 2012
prev sibling next sibling parent Matej Nanut <matejnanut gmail.com> writes:
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.

Jan 13 2012
prev sibling next sibling parent Matej Nanut <matejnanut gmail.com> writes:
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)



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

Jan 13 2012
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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
Jan 13 2012
prev sibling next sibling parent Justin Whear <justin economicmodeling.com> writes:
On Fri, 13 Jan 2012 23:05:19 +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? And I want to simultaneously advance the string?
 
 "sscanf" seems fiddly and unsafe.
 

Check out readf: http://d-programming-language.org/phobos/ std_stream.html#readf You'll need to wrap the string in a Stream interface (MemoryStream?), but since you're looking for advance-on-read, you want stream semantics anyhow. Justin
Jan 13 2012
prev sibling parent Matej Nanut <matejnanut gmail.com> writes:
I've never noticed std.conv.parse takes a radix argument, silly me.
And will take
a look at readf from std.stream, definitely.

Thanks!

On 14 January 2012 01:20, Justin Whear <justin economicmodeling.com> wrote:
 On Fri, 13 Jan 2012 23:05:19 +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? And I want to simultaneously advance the string?

 "sscanf" seems fiddly and unsafe.

Check out readf: http://d-programming-language.org/phobos/ std_stream.html#readf You'll need to wrap the string in a Stream interface (MemoryStream?), but since you're looking for advance-on-read, you want stream semantics anyhow. Justin

Jan 14 2012