Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 12746
  • Last Modified:

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
0
aletheia1
Asked:
aletheia1
  • 4
  • 2
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now