473,473 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
9 54139
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
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
Randy Webb wrote:
var b = parseInt(anyValue) & 255;
var b = parseInt(anyValue,10) & 255;


Of course.

ciao, dhgm
Jul 23 '05 #4
"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
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
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
"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
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
Thanks for the explaination.
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: ern | last post by:
Anybody know a quick and dirty function for going from "547.6458679" to the integer version 548 ? i.e. int returnIntegerEquivalent(char * charNum){ int intNum; //Do some stuff... return...
1
by: Vaj | last post by:
Hi, when i'm Validating the Age TextBox like this if(txtAge.Text>125) reqage.ErrorMessage="age should be less than 125"; . I'm getting an errormessage "cannot use > to compare a string with...
13
by: coinjo | last post by:
Is there any function to determine the length of an integer string?
5
by: Joergen Bech | last post by:
Basically, I want to convert hex values in the range "00000000" to "FFFFFFFF" to a signed, 32-bit Integer value. In VB6, I could just write lngValue = Val(hexstring$). In VB.Net, I seem to be...
8
by: Alex Fraser | last post by:
Can negating a non-negative signed integer value ever overflow? Put another way, can it be true that (mathematically) INT_MIN > -INT_MAX, LONG_MIN > -LONG_MAX etc? I know that typically it can't...
4
by: philin007 | last post by:
Hi , I have the following javascript codes: ****************************************** <script language="JavaScript"> <!-- .... ..... if (nextRow >5) {
3
by: rayapati | last post by:
dear all, i had a problem in my program. that is we need to change the entire string into the positive integer value plase tell me how to do that one as soon as posible . thank you.
3
by: Sanjay | last post by:
Hi Everyone, I am converting an image file into a Java string (via an applet) and would like to pass the Java string back to the Javascript function that calls the applet's. I know the simple...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.