?
Solved

Casting in JavaScript

Posted on 2005-03-16
6
Medium Priority
?
12,741 Views
Last Modified: 2012-06-27
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
Comment
Question by:aletheia1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 5

Expert Comment

by:steve_bagnall
ID: 13553277
Hi,

Try

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

Cheers,
Steve
0
 
LVL 5

Expert Comment

by:steve_bagnall
ID: 13553279
Ouch .. sorry

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

Author Comment

by:aletheia1
ID: 13553316
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 5

Accepted Solution

by:
steve_bagnall earned 375 total points
ID: 13553488
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
 
LVL 5

Expert Comment

by:steve_bagnall
ID: 13553510
Just thought you may need a combo of both try this also:

d2 = d2 + ""
startKey = ((d2.substr(i,1) - 0) + startKey) * multKey + addKey;
0
 

Author Comment

by:aletheia1
ID: 13553530
Hi Steve,
actually your solution works :-)
Thank you so much.
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question