Casting in JavaScript

Hi, I'm writing a JavaScript function but I'm doing a mess with casting.

startKey = (parseInt(d2.substr(i,1)) + startKey) * multKey + addKey;

All the variables are numbers, except d2 that is a string.
I want to convert d2.substr(i,1) in a number, but it doesn't work...?

Thanks in advance
aletheia1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

steve_bagnallCommented:
Hi,

Try

(d2.substr(i,1) - 1)

Cheers,
Steve
0
steve_bagnallCommented:
Ouch .. sorry

(d2.substr(i,1) - 0)
0
aletheia1Author Commented:
No... it doesn't work.
"method or property not supported".

This is my whole code, maybe it can help:

function Encrypt()
{
   
      var startKey = 10000;
      var multKey = 15000;
      var addKey = 20000;
      var d1 = new String();
      var d2 = new String();
      
      d1 = document.fcrypt.campo1.value;
      d2 = "";
      for (i=0;i<=d1.length-1;i++)
      {
          alert(d1.substr(0,1));
            d2 = d2 + d1.substr(i,1) ^ (startKey>>8);
            startKey = ((d2.substr(i,1)-0) + startKey) * multKey + addKey;
      }
      
      document.fcrypt.campo2.value = d2;
      
}
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

steve_bagnallCommented:
Hi,

Yeah d2 is a number as you implicitly cast it to one when you use ^ so you need to change it to a string again.  But beware as your logic may then be wrong as d2 = d2 + d1.substr(i,1) ^ (startKey>>8); will numerically add d1.substr(i,1) ^ (startKey>>8) to d2.

Possible solution:
d2 = d2 + d1.substr(i,1) ^ (startKey>>8);
d2 = d2 + ""
startKey = ((d2.substr(i,1)) + startKey) * multKey + addKey;

Cheers,
Steve.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
steve_bagnallCommented:
Just thought you may need a combo of both try this also:

d2 = d2 + ""
startKey = ((d2.substr(i,1) - 0) + startKey) * multKey + addKey;
0
aletheia1Author Commented:
Hi Steve,
actually your solution works :-)
Thank you so much.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.