<div class="codetitle"> <span><a style="CURSOR: pointer" data="9394" class="copybut" id="copybut9394" onclick="doCopy('code9394')"><u>复制代</u></a></span>代码如下:</div> <div class="codebody" id="code9394"> <br><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <BR><html xmlns="http://www.w3.org/1999/xhtml"> <br> <br><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <br><title>10进制<-->2进制</title> <br></head> <br><br> <br>10 進数: <br> <input type="text" id="10 進数" /> <br> <input type="button" value="to Binary" onclick="return toBinary();" /> <br /> <br>バイナリ: <br> <input type="text" id="binary" /> <br> <input type="button" value="10 進数へ" onclick="return toDecimal();" /> <br><br> <BR><script type="text/javascript"> <br>var d = document.getElementById('10 進数'); <br>var b = document.getElementById('binary'); <br><br>関数 toBinary() { <br> var num = d.value; <br> if (isNaN(num) || !num) { <br> d.value = ""; <br> false を返します。 <br> } <br> b.value = (parseInt(num)).toString(2); <br>} <br><br>関数 toDecimal() { <br> var num = b.value; <br> if (isNaN(num) || !num) { <br> b.value = ""; <br> false を返します。 <br> } <br> d.value = parseInt(num, 2); <br>} <br></script> <br></body> <br></html> <br> </div>