...

View Full Version : help with parseInt()



corley
06-16-2003, 04:24 AM
Right now I am in the process of writing a script that will convert a string into an XOR encryption. I am trying to convert it from a Javascript script I found and I am using PHP to do the conversion.

There is a Javascript tidbit that for some reason I am unable to duplicate, so I am wondering whats the difference between one version I can, and one I can't.

I am unable to successfully duplicate the resultant from the following in PHP:
var mult =parseInt(rand.charAt(sPos*2) + rand.charAt(sPos*3));

however, I am able to duplicate the following I believe:
var mult = parseInt(rand.charAt(sPos*2)) + parseInt(rand.charAt(sPos*3);

If it is relevant, the PHP code is:
$mult = substr($rand, $sPos*2, 1) + substr($rand, $sPos*3, 1);

The last two match up perfectly in all my tests, however.

So pretty much my question is, whats the difference? TIA.

glenngv
06-16-2003, 04:38 AM
this will concat the 2 characters before making it a number since charAt returns a string not a number.

var mult =parseInt(rand.charAt(sPos*2) + rand.charAt(sPos*3));

e.g.

mult = parseInt("1" + "2")
mult = parseInt(12)
mult = 12

where as

var mult = parseInt(rand.charAt(sPos*2)) + parseInt(rand.charAt(sPos*3);

will result in:

mult = parseInt("1") + parseInt("2")
mult = 1+2
mult = 3

corley
06-16-2003, 05:15 AM
thank you so much. that clarifies everything. thanks again.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum