By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,772 Members | 1,272 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,772 IT Pros & Developers. It's quick & easy.

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

P: n/a
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;"
Jul 23 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Henrik wrote:
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
Jul 23 '05 #2

P: n/a
Dietmar Meier wrote:
Henrik wrote:
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;


var b = parseInt(anyValue,10) & 255;

parseInt without a radix is asking for problems.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #3

P: n/a
Randy Webb wrote:
var b = parseInt(anyValue) & 255;
var b = parseInt(anyValue,10) & 255;


Of course.

ciao, dhgm
Jul 23 '05 #4

P: n/a
"Dietmar Meier" <us***************@innoline-systemtechnik.de> wrote in message news:<3b*************@individual.net>...
Randy Webb wrote:
var b = parseInt(anyValue) & 255;

var b = parseInt(anyValue,10) & 255;


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
Jul 23 '05 #5

P: n/a
Henrik wrote:
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
Jul 23 '05 #6

P: n/a
JRS: In article <2b**************************@posting.google.com >,
dated Thu, 7 Apr 2005 03:56:07, seen in news:comp.lang.javascript,
Henrik <he******@home.se> posted :
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #7

P: n/a
"Dietmar Meier" <us***************@innoline-systemtechnik.de> wrote in message news:<3b*************@individual.net>...
Henrik wrote:
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!!
Jul 23 '05 #8

P: n/a
JRS: In article <2b**************************@posting.google.com >,
dated Thu, 7 Apr 2005 12:21:22, seen in news:comp.lang.javascript,
Henrik <he******@home.se> posted :

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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #9

P: n/a
Thanks for the explaination.
Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.