JRS: In article <
[email protected]>,
dated Thu, 7 Apr 2005 12:21:22, seen in
Henrik said:
Hi and thanks to both of you.. Here is my problem more detailed i
cannot understand how to do this in js.
// The result so far in previous calc, same result in js below
int p1= 68223947;
int p2= -1450742052;
//result (java)
byteArray[0] = new Integer(p1 >> (24)).byteValue(); // =4
byteArray[1] = new Integer(p1 >> (16)).byteValue(); // =17
byteArray[2] = new Integer(p1 >> (8)).byteValue(); // =3
byteArray[3] = new Integer(p1 >> (0)).byteValue(); // = -53
Well, I'd still prefer it in English, rather than Java.
ISTM that you are there taking integers p1, p2 apart into their
constituent bytes.
In that case it is utter folly to consider parseInt, since that requires
a string; and unnecessary to use unary +.
See <URL:
http://www.merlyn.demon.co.uk/js-logic.htm>.
bA = [] // suffices to make an array
bA[0] = p1 >>> 24 & 0xFF // 4
bA[1] = p1 >>> 16 & 0xFF // 17
bA[2] = p1 >>> 08 & 0xFF // 3
bA[3] = p1 >>> 00 & 0xFF // 203
If you really do want results in the range -128..+127, then
bA[0] = (n = p1 >>> 24 & 0xFF)<0x80 ? n : n-0x100
bA[1] = (n = p1 >>> 16 & 0xFF)<0x80 ? n : n-0x100
bA[2] = (n = p1 >>> 08 & 0xFF)<0x80 ? n : n-0x100
bA[3] = (n = p1 >>> 00 & 0xFF)<0x80 ? n : n-0x100
or
function F(x, m) { var n
return (n = x >>> 8*m & 0xFF)<0x80 ? n : n-0x100 }
bA[0] = F(p1, 3) // 4
bA[1] = F(p1, 2) // 17
bA[2] = F(p1, 1) // 3
bA[3] = F(p1, 0) // -53
Above, >> will work just as well as >>> !
or for (j=0, k=3 ; j<4 ; ) bA[j++] = F(p1, k--)
or j=0 ; k=3 ; while (j<4) bA[j++] = F(p1, k--)
However, if it should be that you want to present the results as hex
strings,
S = (p1+0x100000000).toString(16)
and use substr or substring to extract.