B
beach.dk
Hi,
I'm trying to implement a simple hash algorith called rs_hash in
javascript,
but I cannot get a correct result.
In c the code looks like this:
signed char myArray[6];
myArray[0]= 0x01;
myArray[1]= 0x02;
myArray[2]= 0x03;
myArray[3]= 0x04;
myArray[4]= 0x05;
myArray[5]= 0x06;
signed int b = 378551;
signed int a = 63689;
signed int hash = 0;
int i= 0;
int len= 6;
for( i= 0; i<len; i++ )
{
hash = hash * a + myArray;
a = a * b;
}
return hash;
and my try in javascripts looks like this
var myArray = new Array();
myArray[0] = 0x01;
myArray[1] = 0x02;
myArray[2] = 0x03;
myArray[3] = 0x04;
myArray[4] = 0x05;
myArray[5] = 0x06;
var b = 378551;
var a = 63689;
var hash = 0;
var i = 0;
var len= 6;
for( i== 0; i<len; i++)
{
hash = hash * a + myArray;
a = a * b;
}
return hash;
The c functions returns -1596271655 (2698695641 when casted to
unsigned)
while the the javacsript funtion returns 4.922527108004972e+107
I guess this has to do with the fact that javascript has typeless
variables, but is there a way to
cast them to signed integers?
I tried casting some variables
var b = parseInt('378551');
var a = parseInt('63689');
var hash = parseInt('0');
but this didn't help. My some out there can?
I'm trying to implement a simple hash algorith called rs_hash in
javascript,
but I cannot get a correct result.
In c the code looks like this:
signed char myArray[6];
myArray[0]= 0x01;
myArray[1]= 0x02;
myArray[2]= 0x03;
myArray[3]= 0x04;
myArray[4]= 0x05;
myArray[5]= 0x06;
signed int b = 378551;
signed int a = 63689;
signed int hash = 0;
int i= 0;
int len= 6;
for( i= 0; i<len; i++ )
{
hash = hash * a + myArray;
a = a * b;
}
return hash;
and my try in javascripts looks like this
var myArray = new Array();
myArray[0] = 0x01;
myArray[1] = 0x02;
myArray[2] = 0x03;
myArray[3] = 0x04;
myArray[4] = 0x05;
myArray[5] = 0x06;
var b = 378551;
var a = 63689;
var hash = 0;
var i = 0;
var len= 6;
for( i== 0; i<len; i++)
{
hash = hash * a + myArray;
a = a * b;
}
return hash;
The c functions returns -1596271655 (2698695641 when casted to
unsigned)
while the the javacsript funtion returns 4.922527108004972e+107
I guess this has to do with the fact that javascript has typeless
variables, but is there a way to
cast them to signed integers?
I tried casting some variables
var b = parseInt('378551');
var a = parseInt('63689');
var hash = parseInt('0');
but this didn't help. My some out there can?