Tuesday, 15 September 2015

DECIMAL TO BINARY CONVERTER CODE


-----------------------------------------------------------------------------------------------------------------
COPY AND PASTE BELOW CODE IN POST HTML SECTION
------------------------------------------------------------------------------------------------------------------

<html>
<head>

<style type="text/css">
input { font-size: 1em; }
@media (max-width:799px) {
.btn { font-size: 1.2em; padding:7px; }
}
@media (min-width:800px) {
.btn { font-size: 1.2em; padding:4px; }
}
.intext {
background:url(data:image/gif;base64,R0lGODlhsQBfALMAAO+9jPe9iP+9e/+9hOLBnOnBnMbGwdbGs63G3rXG1r3GzrnG2qXG563G56XG75zG9ywAAAAAsQBfAAAE/lDISau9OOvNZQBgKI5BZ55oqq7s+QHEcRi0gtyMo+98z9wIBc0gI4AGraRyyex8YjbE7kGtWq/YbFb3SygORmRzTC5bPjObQ8tuu9+PnzBsrttPsIMazu/7q3JgAHeEdzAGCAx/i4x+DgwJBgQlhZUsATEJio2cnX0MXwCUlqQYAXoNa56rrG8OCJKjpZaYBg2tuLlvsAWzhQAHUrrDxFsJB4O+Y6cKqsXP0FSgBGLKl8HR2doPCdTWKcAI2+PZx7LfGAAGm+Tt0AgH5+gS6uzu98/w8sqn9vj/xbrNI5AAoMFoDhQkmxXAgLODEIkxODCrQMGIGJ8ptHTAX8aP/rgQECDU8CHIk60cUKxjEaXLYRvJdHxJM5fIMQ5r6ky5UomBnUBZqfQZtKinoSx+Gl3ayMFIFQdMMp0Kp8HTE1GpavVjFavUrWC1NOjFAYDHsGixJNhXQUHat216YjgAt24WBmQvmLXL18paDG77Cn4gdwKBwYMZLJxwEXHfwgC+Ok6LQJbSyXydTgggDnNfA4Y9C25A6bJou08Dn7a7svPquqAFuH79VoEAzrTrIhAQOTdcBh8k+57qAITw4UsV90YeFsGAvczBJpAwO/rU2KatM316WPtUxfTOetc5fUL18TtjS8iOvqaDvLzFtz9puwL7+SffW4CO32V9C/f1/heRfnrJJyBA/11A14EZ4bVBgAy6g5QGATQW4T/qbVDALRfiowBbFxBwXIfFILBYBwuSuI1mKkCoYi4TQjXii510lURWNOpioxIEcJgjKwnAx6OPP3KSwIlLtFRkIwaAmAQAqi3JhwMZ1kHAeVJmEaQl6sz440ROmkFQlljE5Es/ZD5w0zwFRJkjlUh+MyaNCQk5jwQDENBMh3XeqaECBloHip1+poPIfLDEWSiFenqJmQPdhLmoBgXYMlwDBhA6qQqYAHraNJJumkItqQyGqRGikkSAAZq8BWksqc5ySCLEBYFMNbFac4gmjv7zSCTIhJqrrAVAkUAivUqEgBeSLRSg6LC55pEGDsnC8cgNkRCBKrTcqgBAsTIMcSwQ5JaL7RDaOtvtuuy2O0EEAAA7);
font-family:'Comic Sans MS';
font-weight:bold;
}
.outtext {
background:url(data:image/gif;base64,R0lGODlhsQBfALMAAO+9jPe9iP+9e/+9hOLBnOnBnMbGwdbGs63G3rXG1r3GzrnG2qXG563G56XG75zG9ywAAAAAsQBfAAAE/lDISau9OOvNZQBgKI5BZ55oqq7s+QHEcRi0gtyMo+98z9wIBc0gI4AGraRyyex8YjbE7kGtWq/YbFb3SygORmRzTC5bPjObQ8tuu9+PnzBsrttPsIMazu/7q3JgAHeEdzAGCAx/i4x+DgwJBgQlhZUsATEJio2cnX0MXwCUlqQYAXoNa56rrG8OCJKjpZaYBg2tuLlvsAWzhQAHUrrDxFsJB4O+Y6cKqsXP0FSgBGLKl8HR2doPCdTWKcAI2+PZx7LfGAAGm+Tt0AgH5+gS6uzu98/w8sqn9vj/xbrNI5AAoMFoDhQkmxXAgLODEIkxODCrQMGIGJ8ptHTAX8aP/rgQECDU8CHIk60cUKxjEaXLYRvJdHxJM5fIMQ5r6ky5UomBnUBZqfQZtKinoSx+Gl3ayMFIFQdMMp0Kp8HTE1GpavVjFavUrWC1NOjFAYDHsGixJNhXQUHat216YjgAt24WBmQvmLXL18paDG77Cn4gdwKBwYMZLJxwEXHfwgC+Ok6LQJbSyXydTgggDnNfA4Y9C25A6bJou08Dn7a7svPquqAFuH79VoEAzrTrIhAQOTdcBh8k+57qAITw4UsV90YeFsGAvczBJpAwO/rU2KatM316WPtUxfTOetc5fUL18TtjS8iOvqaDvLzFtz9puwL7+SffW4CO32V9C/f1/heRfnrJJyBA/11A14EZ4bVBgAy6g5QGATQW4T/qbVDALRfiowBbFxBwXIfFILBYBwuSuI1mKkCoYi4TQjXii510lURWNOpioxIEcJgjKwnAx6OPP3KSwIlLtFRkIwaAmAQAqi3JhwMZ1kHAeVJmEaQl6sz440ROmkFQlljE5Es/ZD5w0zwFRJkjlUh+MyaNCQk5jwQDENBMh3XeqaECBloHip1+poPIfLDEWSiFenqJmQPdhLmoBgXYMlwDBhA6qQqYAHraNJJumkItqQyGqRGikkSAAZq8BWksqc5ySCLEBYFMNbFac4gmjv7zSCTIhJqrrAVAkUAivUqEgBeSLRSg6LC55pEGDsnC8cgNkRCBKrTcqgBAsTIMcSwQ5JaL7RDaOtvtuuy2O0EEAAA7);
font-family:'Comic Sans MS';
font-weight:bold;
}
a:link { text-decoration: none; color: #083d8d; }
a:visited { text-decoration: none; color: #083d8d; }
a:active { text-decoration: underline; color: #000000; }
a:hover { text-decoration: underline; color: #000000; }
a:focus { outline: none; }

.math { font-family: "Times New Roman"; font-size: 1.6em; padding-left: 6px; font-weight: normal;}
.mathsymbol { font-family: "Times New Roman"; font-size: 1.2em; font-weight: normal; }
.text { font-family: "Times New Roman", Times, serif; font-size: 1.2em }
.code { font-family: "Courier New"; font-size: 1.0em; padding-left: 6px;}
.codebg { font-family: "Courier New"; font-size: 1.0em; color: #000000; background:#f0f0f0; margin-top: 0px; margin-bottom: 0px; padding: 10px; }
.codebg.p { font-family: "Courier New"; font-size: 1.0em; color: #000000; background:#f0f0f0; margin-top: 0px; margin-bottom: 0px;}
p.code { font-family: "Courier New"; font-size: 1.0em; color: #000000; background:#f0f0f0; margin:0px; padding:5px; border-width:1px; border-color:#ccc; border-style:solid; line-height:20px}
p.wcode { font-family: "Courier New"; font-size: 1.0em; color: #000000}
p.tcode { font-family: "Courier New"; font-size: 1.0em; color: #000000; background:#f0f0f0; margin:0px; padding:5px; border-width:1px; border-color:#ccc; border-style:solid; line-height:20px }

.table1 .hov { border: 2px black solid; padding: 4px; }
.table1 .ho2 { border: 2px #707070 solid; padding: 4px; }
.table1 .td { border: 1px black solid; padding: 5px; }

table.dtable, table.ntable { border: 1px #ccc solid; border-collapse:collapse; padding: 5px;}
table.dtable th, table.dtable td, table.ntable th, table.ntable td { border: 1px #ccc solid; border-collapse:collapse; padding: 5px; font-weight:normal;}
table.dtable th, table.ntable th { text-align:center }
table.ntable td { text-align:right }

table.calc, .calc2 {
border-spacing: 0px;
border-collapse: separate;
background: #e8e8e8;
border:1px #ddd solid;
border-radius:7px;
-moz-border-radius:7px;
-webkit-border-radius:7px;
}
table.calc td, .calc2 td {
padding: 2px;
border-width: 0px;
border-style: none;
background: #e8e8e8;
}
.calc2 select { padding:4px 2px; font-size:1.2em; font-family:Arial, Helvetica, sans-serif }
@media all and (max-width: 576px) {
.calc2 { padding:8px; margin-left:auto; margin-right:auto; }
.calc2 td { float:left; }
.calc2 td:nth-child(2) { clear:left; }
.calc2 tr td:nth-child(3) { padding-top:10px; }
.calc2 input[type=text], .calc2 input[type=number], .calc2 input[type=url] { width:190px; font-size:x-large; padding-left:3px; }
.calc2 tr td:nth-child(3) select { margin-top:-8px; }
}
@media all and (min-width: 577px) {
.calc2 { padding:15px; }
.calc2 tr { line-height:40px; }
.calc2 input[type=text], .calc2 input[type=number], .calc2 input[type=url] { width:220px; font-size:x-large; padding-left:3px; }
}

#area {
font-size:x-large;
width:100%;
}
</style>

<script type='text/javascript'>
function setfocus() {
document.calcform.x.focus();
}
function calc() {
x = document.calcform.x.value;
y = convert(x);
y = roundresult(y);
document.calcform.y.value = y;
}
function calctest() {
x = document.calcform.x.value;
y = convert(x);
//y = roundresult(y);
y = roundresult1(y);
document.calcform.y.value = y;
}
function calc3() {
x1 = document.calcform.x1.value;
x2 = document.calcform.x2.value;
y = convert(x1,x2);
y = roundresult(y);
document.calcform.y.value = y;
}
function calc4() {
x1 = document.calcform.x1.value;
x2 = document.calcform.x2.value;
x3 = document.calcform.x3.value;
y = convert(x1,x2,x3);
y = roundresult(y);
//yy = y.toString();
//if( yy.length>12 ) {
// y = parseFloat(y);
// y = y.toPrecision(10);
//}
document.calcform.y.value = y;
}
function calc5() {
x = document.calcform.x.value;
y = convert1(x);
y = roundresult(y);
document.calcform.y1.value = y;
y = convert2(x);
y = roundresult(y);
document.calcform.y2.value = y;
}
function calcbase(b1,b2) {
x = document.calcform.x.value;
document.calcform.y.value = convertbase(x,b1,b2);
}
function calcbase2() {
x = document.calcform.x.value;
y = convert(x);
document.calcform.y.value = y;
}

function roundresult(x) {
y = parseFloat(x);
y = roundnum(y,10);
return y;
}
function roundnum(x,p) {
var i;
var n=parseFloat(x);
var m=n.toPrecision(p+1);
var y=String(m);
i=y.indexOf('e');
if( i==-1 ) i=y.length;
j=y.indexOf('.');
if( i>j && j!=-1 )
{
while(i>0)
{
if(y.charAt(--i)=='0')
y = removeAt(y,i);
else
break;
}
if(y.charAt(i)=='.')
y = removeAt(y,i);
}
return y;
}
function removeAt(s,i) {
s = s.substring(0,i)+s.substring(i+1,s.length);
return s;
}

function convertbase(x,b1,b2) {
d="0123456789ABCDEFX";
x=x.replace(/ /,"");
x=x.toUpperCase();
y=0;
// return b1+b2;
for(i=x.length, s=1; i>=1; i--,s*=b1) {
c = x.charAt(i-1);
for(j=0; j<b1+1; j++) {
if( j==b1 ) {
y = "bad input number";
return y;
}
if( c == d.charAt(j) )
break;
}
y+=j*s;
}
x=y;
if( x==0 ) return "0";
y="";
for(s=1; s<=x; s*=b2);
if( x!=s ) s/=b2;
for(p=s; p>=1; p/=b2) {
i = Math.floor(x/p);
y+= d.charAt(i);
x-= i*p;
}
return y;
}
function convertbase2(x,b1,b2) {
x = parseInt(x, b1);
y = x.toString(b2);
return y;
}
var gcd = function(a, b) {
if ( ! b) {
return a;
}
return gcd(b, a % b);
};

function digits_after_period(x)
{
f = x.toString();
i = f.indexOf('.');
len = f.length-i-1;
return len;
}

function parseFraction(s)
{
var n1=n2=0;
var sign=n3=1;
i = s.indexOf(' ');
if( i==0 ) s=s.substring(1);
i = s.indexOf('-');
if( i==0 ) {s=s.substring(1); sign=-1;}
i = s.indexOf(' ');
if( i==0 ) s=s.substring(1);
i = s.indexOf(' ');
if( !isNaN(parseInt(s)) && isFinite(s) ) i=s.length;
if( i!=-1 )
{
n1 = parseInt(s.substring(0,i));
s = s.substring(i+1);
}
i = s.indexOf('/');
if( i!=-1 )
{
n2 = parseInt(s.substring(0,i));
s = s.substring(i+1);
n3 = parseInt(s);
}
return [sign,n1,n2,n3];
}

//]]>
</script>

</head>

<body>

<section id="doc">
<!-- #BeginEditable "doctext" -->

<h1>Decimal to Binary conversion</h1>
<p>Decimal to binary number converter and how to convert.</p>
<h2>Decimal to binary converter</h2>
<p>Enter a decimal number and press the <i>Convert</i> button:</p>

<form name="calcform">
<table class="calc2">
<tbody>
<tr>
<td><label for="x">Enter decimal number:</label></td>
<td class="math"><input type="number" id="x" name="x" class="intext" autofocus tabindex="1"></td>
<td class="mathsymbol"><sub>10</sub></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input onclick="calcbase2()" type="button" value="Convert" class="btn" tabindex="2"> <input onclick="setfocus()" type="reset" value="Reset" class="btn" tabindex="3"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Binary result:</td>
<td class="math"><input type="text" name="y" class="outtext" readonly tabindex="4"></td>
<td class="mathsymbol"><sub>2</sub></td>
</tr>
</tbody>
</table>
</form>


<h2>How to convert decimal to binary</h2>
<p>In order to convert decimal number x to binary number:</p>
<p>For decimal number x:</p>
<ol>
<li>Get the highest power of 2 that is less than the decimal number x:<p class="math">2<i><sup>n</sup></i> &lt; <i>x</i>, (<i>n</i>=1,2,3,...)</li>
<li>The high binary digit is equal 1:<p class="math"><i>d<sub>n</sub></i> = 1</li>
<li>Calculate the difference &#916; of the number x and he power of 2, 2<sup>n</sup>:<p class="math">&#916; = <i>x</i> - 2<i><sup>n</sup></i></li>
<li>Repeat step #1 with the difference until the result is 0.</li>
</ol>
<h4>Example</h4>
<p>Convert x=13 to binary.</p>
<h4>Solution</h4>
<p class="math"><i>n</i>=3, 2<sup>3</sup>=8 &lt; 13</p>
<p class="math"><i>n</i>=4, 2<sup>4</sup>=16 &gt; 13</p>
<p>So</p>
<p class="math"><i>n </i>= 3</p>
<p class="math"><i>d</i><sub>3</sub> = 1</p>
<p class="math">&#916; = 13 - 2<sup>3</sup> = 5</p>
<p class="math"><i>n </i>= 2, <i>x</i> = &#916; = 5</p>
<p class="math"><i>d</i><sub>2</sub> = 1</p>
<p class="math">&#916; = 5 - 2<sup>2</sup> = 1</p>
<p class="math"><i>n </i>= 1, <i>x</i> = &#916; = 1</p>
<p class="math"><i>d</i><sub>2</sub> = 0</p>
<p class="math"><i>n </i>= 0, <i>x</i> = &#916; = 1</p>
<p class="math"><i>d</i><sub>0</sub> = 1</p>
<p class="math">&#916; = 1 - 1 = 0</p>
<p class="math">(<i>d</i><sub>3</sub><i>d</i><sub>2</sub><i>d</i><sub>1</sub><i>d</i><sub>0</sub>)&nbsp; = 1101</p>
<p>So 13 in decimal is equal to 1101 in binary:</p>
<p class="math"><i>x</i> = 13<sub>10</sub> = 1101<sub>2</sub></p>
<p>&nbsp;</p>
<hr>

<script type="text/javascript">
<!--
function convert(x) {
x = parseInt(x);
if( x<0 ) {
if( x>=-32768 )
x+=65536;
else if( x>=-Math.pow(2,31))
x+=Math.pow(2,32);
else if( x>=-Math.pow(2,63) )
x+=Math.pow(2,64);
}
y = x.toString(2);
return(y);
}
//-->
</script>

<!-- #EndEditable -->
</section>


</body>
</html>

0 comments:

Post a Comment

:) :)) ;(( :-) =)) ;( ;-( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ $-) (b) (f) x-) (k) (h) (c) cheer
Click to see the code!
To insert emoticon you must added at least one space before the code.

FB Gadgets | Template Designed by Fatakat PhotosCoolBThemes.com
Code by : paid web directory

https://www.google.co.uk/search?q=site%3Ablogspot.com+fbgadgets