Java > Javascript Integer.parseInt(int value or String).byteValue;

H

Henrik

In Java you can write something like this. Does anyone know how to do
this in javascript?

"byte b=Integer.parseInt(int value or String).byteValue;"
 
D

Dietmar Meier

Henrik said:
In Java you can write something like this. Does anyone know how to do
this in javascript?

"byte b=Integer.parseInt(int value or String).byteValue;"

var b = parseInt(anyValue) & 255;

ciao, dhgm
 
H

Henrik

Dietmar Meier said:
Of course.

ciao, dhgm

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

byteArray[4] = new Integer(p2 >> (24)).byteValue(); // =-87
byteArray[5] = new Integer(p2 >> (16)).byteValue(); // =-121
byteArray[6] = new Integer(p2 >> (8)).byteValue(); // =110
byteArray[7] = new Integer(p2 >> (0)).byteValue(); // -36

// This is my translation to javascript i have tried & 255 as well.
var p1= 68223947;
var p2= -1450742052;

var jsArray= new Array(8);
jsArray[0] = parseInt((p1 >> (24)),10) & 127; //=4 correct
jsArray[1] = parseInt((p1 >> (16)),10) & 127; //=17 correct
jsArray[2] = parseInt((p1 >> (8)),10) & 127; //=3 correct
jsArray[3] = parseInt((p1 >> (0)),10) & 127; //=75 ????? error

jsArray[4] = parseInt((p2 >> (24)),10) & 127; //=41 ????? error
jsArray[5] = parseInt((p2 >> (16)),10) & 127; //=7 ????? error
jsArray[6] = parseInt((p2 >> (8)),10) & 127; //=110 correct
jsArray[7] = parseInt((p2 >> (0)),10) & 127; //=92 ????? error
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Thu, 7 Apr 2005 03:56:07, seen in
Henrik said:
In Java you can write something like this. Does anyone know how to do
this in javascript?

"byte b=Integer.parseInt(int value or String).byteValue;"

Why assume that readers of a javascript newsgroup can understand Java,
which is quite a different language?

Explain the problem in English and/or Swedish, so that you can be
reliably understood. As it is, I am guessing (I'd be guessing Swedish,
too, but there's probably someone here who knows it well enough).



While javascript has a parseInt, you probably do not need it - unless
you want to support octal input.

If the input will actually never exceed 255, you only need b = +S
where S is your "(int value or String)".

If it may do so, and you need to silently discard what might otherwise
be upper bytes, then b = S%256 (if S>=0) or b = S & 0xFF will
suffice.

However, if the input is a user-entered string, you might do well to
validate it with a RegExp, for example to ensure that it consists of at
least one decimal digit, maybe more, but not too many :
<URL:http://www.merlyn.demon.co.uk/js-valid.htm>
Then you need have no concern about the treatment of leading or trailing
characters, numbers like 1.5e+2, etc.

See below.
 
D

Dietmar Meier

Henrik said:
byteArray[3] = new Integer(p1 >> (0)).byteValue(); // = -53

You're looking for the two's complement:

n = parseInt(v, 10) & 255;
if (n > 127) {
n = -((n & 127) ^ 127) - 1;
}

ciao, dhgm
 
H

Henrik

Dietmar Meier said:
Henrik said:
byteArray[3] = new Integer(p1 >> (0)).byteValue(); // = -53

You're looking for the two's complement:

n = parseInt(v, 10) & 255;
if (n > 127) {
n = -((n & 127) ^ 127) - 1;
}

ciao, dhgm


THANKS!! This really did it!! THANKS!! THANKS!! THANKS!!
 
D

Dr John Stockton

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top