Re: atoi equivalents?
- To: mathgroup at smc.vnet.net
- Subject: [mg99407] Re: atoi equivalents?
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Tue, 5 May 2009 05:40:52 -0400 (EDT)
On 5/4/09 at 5:59 AM, michael2718 at gmail.com (Michael) wrote: >What is the most efficient or recommended way to parse numbers in >Mathematica? >I can use ToExpression["1234"] and it will return the number. But >this won't do, because I could be scraping un-trusted data and the >above could lead to an injection of code. I could also use something >like this: ToExpression[StringCases["1234", x1 : NumberString ~~ ___ >-> x1]] but then I have to add error checking and whatnot. I suppose >I could write a simple MathLink program which implement atoi, atol, >etc. >All of these solutions seems like a lot of overhead. Anybody have a >better idea? I really don't see a reason for concern when using ToExpression. Probably, the simplest way to do what you are trying to do would be value=ReadList[s=StringToStream["1234"],Number];Close[s] If the input string is not a number, this code will generate an error. There is still some overhead. But, any method used to do input checking will add overhead.