As usual i searched google to convert Decimal numbers to their Binary equivalents but i couldnt find any short algorithm for that matter. So here is mine..
Decimal to Binary Converter
Deendayal_Hindu commented: var s="213"; function desbin(t){ var num=Number(t); var b=""; while(num>0){ b+=(num%2) var d= num/2; num = Math.floor(d) } return Nu +0
<script type="text/javascript">
function ConvertToBinary(dec) {
var bits = [];
var dividend = dec;
var remainder = 0;
while (dividend >= 2) {
remainder = dividend % 2;
bits.push(remainder);
dividend = (dividend - remainder) / 2;
}
bits.push(dividend);
bits.reverse();
return bits.join("");
}
</script>
Example usage :
<input type="text" id="txtDec" />
<input type="button" value="Convert" onclick="document.getElementById('spBin').innerHTML=ConvertToBinary(document.getElementById('txtDec').value);" />
<span id="spBin"></span>
odessa 0 Newbie Poster
odessa 0 Newbie Poster
redshadowhack 0 Newbie Poster
Deendayal_Hindu -3 Newbie Poster
rproffitt commented: Please be timely. This discussion is 8 years old. -3
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.