binary to decimal array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cirustus
    New Member
    • May 2010
    • 4

    binary to decimal array

    How do I make this array work properly. It is to convert binary to decimal then add them. The example I have written works but I know its not proper for the array as I would like to use a loop with the array to solve the problem

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function sum(a,b)
    {
    
    var c = a.split("");
    var d = b.split("");
    var e;
    var f;
    var answer3;
    e = c[0] * 128 + c[1] * 64 + c[2] * 32 + c[3] * 16 + c[4] * 8 + c[5] * 4 + c[6] * 2 + c[7] * 1;
    f = d[0] * 128 + d[1] * 64 + d[2] * 32 + d[3] * 16 + d[4] * 8 + d[5] * 4 + d[6] * 2 + d[7] * 1;
    answer3 = e + f
    document.write(answer3);
    }
    </script>
    </head>
    <body>
    <form>
    <input type="text" name="a">
    <input type="text" name="b">
    <input type="button" onclick="sum(a.value, b.value)">
    </form>
    </body>
    </html>
    Last edited by Niheel; May 8 '10, 03:51 PM. Reason: punctuation
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    Hi I dont get why you are going for an array. You can achieve this easily why dont you try to find the decimal value for the entered binary value first and then calculate the sum. whatever you are trying here it wont be working out. I am just giving a sample code to convert binary to decimal u enhance it for your requirement
    Code:
    function calDec(val)
    {
    	var tot = 0;
    	var i=0;
    	while(val>1)
    	{
    		var x = val%10;
    		val = val/10;
    		tot+= parseInt(x)*Math.pow(2,i);
    		i++;
    	}
    	alert("total = "+tot);
    }
    Thanks and Regards
    Ramanan Kalirajan

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      it’s even easier:
      Code:
      var x = parseInt(yourValue, 2);

      Comment

      • cirustus
        New Member
        • May 2010
        • 4

        #4
        I understand its easier to do it that way but there must be a way to solve it using an array. I have been told its possible. I like to know from the easiest to the most complex way of doing things gives me more understanding of the language
        Last edited by Niheel; May 8 '10, 03:52 PM. Reason: punctuation, spelling

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          If you want to do complex things, do something with inheritance or cross-browser functionality. You'll be glad for every step you can make as easy as possible.
          Last edited by Niheel; May 8 '10, 03:52 PM. Reason: grammar, punctuation

          Comment

          • cirustus
            New Member
            • May 2010
            • 4

            #6
            yeah but its for my course and it has to be done in an array
            i already know the easy way around it

            Comment

            • RamananKalirajan
              Contributor
              • Mar 2008
              • 608

              #7
              Your array code

              Code:
               function sum(a,b)
               {
               var c = a.split("");
               var d = b.split("");
               var e=0;
               var f=0;
               var answer3;
              for(var i=0;i<c.length;i++)
                e+= parseInt(c[i])*Math.pow(2,i);
              
              for(var i=0;i<d.length;i++)
                f+= parseInt(d[i])*Math.pow(2,i);
               answer3 = e + f
               alert(answer3);
               }
              Thanks and Regards
              Ramanan Kalirajan

              Comment

              Working...