digitalmars.D.learn - Data conversion
- pascal111 (26/26) Nov 16 2021 I used "to" keyword which "std.conv" includes for data
- Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= (6/32) Nov 16 2021 sscanf of C is an option where you cannot use to!T. However, it
- Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= (4/11) Nov 16 2021 Upps
- jfondren (11/15) Nov 16 2021 Strictly speaking it's not a keyword, just a template name.
I used "to" keyword which "std.conv" includes for data conversions, but I think that there are some other ways for data conversions, or maybe there are common ways like casting, I hope to know about. For example, next program are using "to": // D programming language import std.stdio; import std.conv; import std.string; int main() { char[] s; int x=0, e; do{ try{ e=1; write("Enter a numeric value: "); readln(s); s=strip(s); x=to!int(s);} catch (Exception err){ stderr.writefln!"Warning! %s"(err.msg); e=0;} }while(!e); writeln("Correct numeric value!"); return 0; }
Nov 16 2021
On Tuesday, 16 November 2021 at 19:18:50 UTC, pascal111 wrote:I used "to" keyword which "std.conv" includes for data conversions, but I think that there are some other ways for data conversions, or maybe there are common ways like casting, I hope to know about. For example, next program are using "to": // D programming language import std.stdio; import std.conv; import std.string; int main() { char[] s; int x=0, e; do{ try{ e=1; write("Enter a numeric value: "); readln(s); s=strip(s); x=to!int(s);} catch (Exception err){ stderr.writefln!"Warning! %s"(err.msg); e=0;} }while(!e); writeln("Correct numeric value!"); return 0; }sscanf of C is an option where you cannot use to!T. However, it is a c library function, and it doesn't throw an exception on unexpected inputs. https://dlang.org/library/core/stdc/stdio/scanf.html https://www.tutorialspoint.com/c_standard_library/c_function_sscanf.htm
Nov 16 2021
On Tuesday, 16 November 2021 at 19:44:04 UTC, Ferhat Kurtulmuş wrote:On Tuesday, 16 November 2021 at 19:18:50 UTC, pascal111 wrote:Upps https://dlang.org/library/core/stdc/stdio/sscanf.html[...]sscanf of C is an option where you cannot use to!T. However, it is a c library function, and it doesn't throw an exception on unexpected inputs. https://dlang.org/library/core/stdc/stdio/scanf.html https://www.tutorialspoint.com/c_standard_library/c_function_sscanf.htm
Nov 16 2021
On Tuesday, 16 November 2021 at 19:18:50 UTC, pascal111 wrote:I used "to" keyword which "std.conv" includes for data conversions, but I think that there are some other ways for data conversions, or maybe there are common ways like casting, I hope to know about. For example, next program are using "to":Strictly speaking it's not a keyword, just a template name. std.conv is pretty good for conversions like this. You can read about casting at https://dlang.org/spec/expression.html#CastExpression , and I have a quickref for string conversions at https://d.minimaltype.com/index.cgi/wiki?name=string+type+conversions There's a lot to say about string conversions. Phobos date&time conversions deserve a quickref like that, too. On numerical conversions, D is very similar to other languages. OOP casting is noteworthy in that it can fail with `null`.
Nov 16 2021