|
 |
javascript thread: binary to decimal
Message #1 by "Alex Shiell, ITS, EC, SE" <alex.shiell@s...> on Tue, 20 Nov 2001 11:55:54 -0000
|
|
Cheers Greg, it would never have occured to me that there might be more to
parseInt than meets the eye.
(and Elliot thought my solution was simple!)
-----Original Message-----
From: Greg Hill [mailto:ghill@s...]
Sent: 21 November 2001 19:31
To: javascript
Subject: [javascript] Re: binary to decimal
Try:
function bin2dec(bin){
return(parseInt(bin, 2));
}
Then just get rid of your bin2dec function entirely and call parseInt
directly instead.
--Greg
>Yeah, you'd be surprised at how easy some programming tasks are with
>a little bit of planning.
>
>---------------------
>Kev
>
>
>
>----Original Message Follows----
>haha - well this does the job!
>
>-----Original Message-----
>From: O'Hara, Elliott M [mailto:EMOHARA@k...]
>Sent: 21 November 2001 13:15
>To: javascript
>Subject: [javascript] Re: binary to decimal
>
>
>NOOOO...
>Its supposed to be MUCH MUCH harder than that!!
>
>-----Original Message-----
>From: alex.shiell@s... [mailto:alex.shiell@s...]
>Sent: Wednesday, November 21, 2001 7:14 AM
>To: javascript
>Subject: [javascript] Re: binary to decimal
>
>
>Elliot, and anyone else who's interested...
>
>In the absence of any info on an existing function, I've wriiten my own:
>
>function bin2dec(bin){
> s=bin.toString();
> var iResult=0;
> for(var i=0;i<s.length;i++){
> iResult+=(parseInt(s.charAt(i))*Math.pow(2,(s.length-i-1)));
> }
> return(iResult);
>}
>
>
>
>> How do you convert a binary number to decimal in javascript?
>>
>> And indeed how dou you specify that a number is binary and not decimal?
>> (e.g. 1111011 is not to be treated as 1,111,011)
>>
>> ________________________________________________________________________
>> Scottish Enterprise Network
>> http://www.scottish-enterprise.com
>>
>> Headquarters Address & Contact Numbers
>>
>> 150 Broomielaw
>> 5 Atlantic Quay
>> Glasgow
>> G2 8LU.
>> Tel: +44 (0) 141 248 2700.
>> Fax: +44 (0)141 221 3217
>>
>> This message is sent in confidence for the addressee only.
>> It may contain legally privileged information. The contents are not to
>> be disclosed to anyone other than the addressee. Unauthorised recipients
>> are requested to preserve this confidentiality and to advise the sender
>> immediately of any error in transmission.
>>
>>
>
>
>
>________________________________________________________________________
>Scottish Enterprise Network
>http://www.scottish-enterprise.com
>
>Headquarters Address & Contact Numbers
>
>150 Broomielaw
>5 Atlantic Quay
>Glasgow
>G2 8LU.
>Tel: +44 (0) 141 248 2700.
>Fax: +44 (0)141 221 3217
>
> This message is sent in confidence for the addressee only.
>It may contain legally privileged information. The contents are not to
>be disclosed to anyone other than the addressee. Unauthorised recipients
>are requested to preserve this confidentiality and to advise the sender
>immediately of any error in transmission.
>
>
>
>
>
>
>_________________________________________________________________
>Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
>
>
________________________________________________________________________
Scottish Enterprise Network
http://www.scottish-enterprise.com
Headquarters Address & Contact Numbers
150 Broomielaw
5 Atlantic Quay
Glasgow
G2 8LU.
Tel: +44 (0) 141 248 2700.
Fax: +44 (0)141 221 3217
This message is sent in confidence for the addressee only.
It may contain legally privileged information. The contents are not to
be disclosed to anyone other than the addressee. Unauthorised recipients
are requested to preserve this confidentiality and to advise the sender
immediately of any error in transmission.
|
|
 |