Click to See Complete Forum and Search --> : how to vonvert char to int?


gj596
01-09-2003, 11:42 PM
Dear all:

Please help! I need to do a hash for a string. But I cannot figure out how to find out the ASCII code of a char (or convert char to int). How to do it in javascript?

e.g., the Java code:

int hash=0;
for(int i=0;i<mystring.length();i++)
{
hash=hash<<3;
hash+=(int)mystring.charAt(i);
}

Thank you very much!

khalidali63
01-10-2003, 01:31 AM
Code snippet below will do that .

Khalid



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<script>
var myString="A lazy bum fox jumps over a stupid dog."
for(x=0;x<myString.length;x++){
var chr = myString.charAt(x);
alert("Char is "+chr+", integer val = "+chr.charCodeAt(chr))
}
</script>
</body>
</html>