Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 4 of 4 total
Thread *** StrToInt BUG
Wed, Aug 1 2012 5:35 AMPermanent Link

Mark Brooks

Slikware

Avatar

Hi TIm,

I have found a very obscure bug in the StrToInt function. This came about today, August 1st, when I was parsing some strings to extract year, day and month. The month came back as '08' (with a leading zero) which I passed to StrToInt. The result was 0 not 8!

This is because StrToInt uses the JavaScript function parseInt, which in turn assumes base 8 (octal) for strings with leading zeros, unless you tell it otherwise. I think you need to add a second parameter (10) to your parseInt call to force base 10. Details below from w3schools:

-------------------------------------------------------------------

Definition and Usage

The parseInt() function parses a string and returns an integer.

The radix parameter is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number.

If the radix parameter is omitted, JavaScript assumes the following:

If the string begins with "0x", the radix is 16 (hexadecimal)
If the string begins with "0", the radix is 8 (octal). This feature is deprecated ***** PROBLEM *****
If the string begins with any other value, the radix is 10 (decimal)
Syntax

parseInt(string, radix)

Parameter   Description
string   Required. The string to be parsed
radix   Optional. A number (from 2 to 36) that represents the numeral system to be used

Hope this makes sense
Cheers
Mark
Thu, Aug 2 2012 2:02 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Mark,

<< I have found a very obscure bug in the StrToInt function. This came about
today, August 1st, when I was parsing some strings to extract year, day and
month. The month came back as '08' (with a leading zero) which I passed to
StrToInt. The result was 0 not 8!

This is because StrToInt uses the JavaScript function parseInt, which in
turn assumes base 8 (octal) for strings with leading zeros, unless you tell
it otherwise. I think you need to add a second parameter (10) to your
parseInt call to force base 10. Details below from w3schools: >>

Yep, thanks, I'll make sure this is corrected.

BTW, didn't you know that W3Schools is the devil ?  That's a Stack Overflow
joke. Smile

Tim Young
Elevate Software
www.elevatesoft.com
Fri, Aug 3 2012 3:50 AMPermanent Link

Mark Brooks

Slikware

Avatar

<< BTW, didn't you know that W3Schools is the devil ?  That's a Stack Overflow
joke. Smile>>

Forgive me, oh wise one, I am still learning this stuff and the "dark side" is all around ............
Fri, Aug 3 2012 1:00 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Mark,

<< Forgive me, oh wise one, I am still learning this stuff and the "dark
side" is all around ............ >>

Just to clarify - that's not *me* saying that.  I use W3Schools all the time
to look stuff up (it's always the first Google result normally), but if you
mention that on Stack Overflow you'll get the ruler across the knuckles from
the purists that swear that it's chock full of inaccuracies. Smile

Tim Young
Elevate Software
www.elevatesoft.com
Image