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