Solved

# Casting in JavaScript

Posted on 2005-03-16
Medium Priority
12,741 Views
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...?

0
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
• 4
• 2

LVL 5

Expert Comment

ID: 13553277
Hi,

Try

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

Cheers,
Steve
0

LVL 5

Expert Comment

ID: 13553279
Ouch .. sorry

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

Author Comment

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 d1 = new String();
var d2 = new String();

d1 = document.fcrypt.campo1.value;
d2 = "";
for (i=0;i<=d1.length-1;i++)
{
d2 = d2 + d1.substr(i,1) ^ (startKey>>8);
startKey = ((d2.substr(i,1)-0) + startKey) * multKey + addKey;
}

document.fcrypt.campo2.value = d2;

}
0

LVL 5

Accepted Solution

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

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

ID: 13553530
Hi Steve,
Thank you so much.
0

## Featured Post

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
Course of the Month11 days, 18 hours left to enroll