JS binary and decimal conversion codes

Source: Internet
Author: User

JS binary and decimal conversion code:
Decimal and binary conversion is often used in the code, the following is a code example, I hope to be able to bring help to the needs of friends.
The code example is as follows:

<!DOCTYPE HTML><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><Metaname= "Author"content= "http://www.softwhy.com/" /><title>Ant Tribe</title><Scripttype= "Text/javascript">  varD;varb;functiontobinary () {varNum=D.value; if(IsNaN (num)||!num) {D.value=""; return false; } b.value=(parseint (num)). toString (2); } functionToDecimal () {varNum=B.value; if(IsNaN (num)||!num) {B.value=""; return false; } d.value=parseint (num,2); } window.onload=function() {D=document.getElementById ('decimal'); b=document.getElementById ('binary'); varDECIMALBT=document.getElementById ("DECIMALBT"); varBINARYBT=document.getElementById ("BINARYBT"); Decimalbt.onclick=function() {tobinary ()} Binarybt.onclick=function() {ToDecimal ()}}</Script></Head><Body>decimal:<inputtype= "text"ID= "decimal" /><inputtype= "button"value= "to Binary"ID= "DECIMALBT" /><BR/>Binary :<inputtype= "text"ID= "binary" /><inputtype= "button"value= "to Decimal"ID= "BINARYBT"/></Body></HTML>

The above code realizes the conversion between binary and decimal, in fact, it is very simple, because JS itself provides such a function.
Related reading:
The 1.isNaN () function can be found in the IsNaN () method section of JavaScript .
The 2.parseInt () function can be found in the parseint () function section of JavaScript .
3. The ToString () function can refer to the ToString () method section of the JavaScript number object.

The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=9794

For more information, refer to: http://www.softwhy.com/javascript/

JS binary and decimal conversion codes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.