PDA

View Full Version : Binary Converter



mburt
08-18-2006, 05:30 AM
Okay.

I've worked out a script that converts binary to decimal, but I was, originally looking for the opposite effect. So now, I have a binary to decimal code, but I need a decimal to binary code.

Here it is:


<html>
<head>
<script type="text/javascript">
function convert(string) {
var extra = 8 - string.value.length
for (i=0;i<=extra;i++) {
if (string.value.length<8) {string.value=0+string.value}
}
var num
var unum=128*2
var add
var pos
var binary
for (i=1;i<=string.value.length;i++) {
unum=unum/2
add=unum
if (string.value.charAt(i-1)==0) {continue}
binary+=add+"+"
}
alert(eval(binary.substring(9,binary.length-1)))
}
</script>
</head>
<body>
<input id="code" value="00000010" maxlength="8">
<br><input type="button" value="Convert" onclick="convert(code)">
</body>
</html>

shachi
08-18-2006, 06:04 AM
You could have simply done this:



<html>
<head>
<script type="text/javascript">
function convert() {
var string = document.testform.decimal.value;
var bin = parseInt(string, 2);
alert(bin);
}
</script>
</head>
<body>
<form name="testform">
<input id="code" value="00000010" maxlength="8" name="decimal">
<br><input type="button" value="Convert" onclick="convert()">
</form>
</body>
</html>

mburt
08-18-2006, 01:28 PM
That still doesn't answer my original question though, but thanks :)

mwinter
08-18-2006, 06:30 PM
I've worked out a script that converts binary to decimal,

As shachi has pointed out, the parseInt function also provides that function. It is specified to accept any base from 2 to 36.



but I was, originally looking for the opposite effect.

Use the Number.prototype.toString method:



(10).toString(2) // '1010'




alert(eval(binary.substring(9,binary.length-1)))

What on Earth is that eval function call for? If you're trying to convert from
String to Number, there are four good ways to do it, four not so good, but the eval function isn't even a consideration.

Mike

mburt
08-18-2006, 09:11 PM
If you didn't have the "eval" function the string would turn out the equation.

Ex:
10100100

would be 164,
but without eval,
this is what would happen

"128+32+4"

mwinter
08-18-2006, 09:38 PM
If you didn't have the "eval" function the string would turn out the equation.

I must admit that I didn't look that closely at the code; the eval call just jumped out at me. However, that then begs the question: why not just sum the numbers (a running total)? Using eval is still unnecessary.

A much simpler implementation that could handle any 32-bit binary string is:



function bin2dec(binary) {
var value = 0;

for (var i = 0; i < binary.length; ++i) {
value <<= 1;
value += +binary.charAt(i);
}
return value;
}

The 32-bit limit is introduced by the left shift (assignment) operator as it converts its left-hand argument to 32-bit signed integer (and evaluates to the same). It could be replaced by simple multiplication by two (2), but using the parseInt function makes this all academic, anyway.

Mike