Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2003
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help with parseInt()

    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.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    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
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New to the CF scene
    Join Date
    Jun 2003
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you so much. that clarifies everything. thanks again.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •