# Thread: Binary Converter

1. ## Binary Converter

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:

Code:
```<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>```
Last edited by mburt; 08-18-2006 at 05:49 AM.

2. Senior Coders
Join Date
Aug 2005
Posts
971
Thanks
0
Thanked 0 Times in 0 Posts
You could have simply done this:

Code:
```<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>```

3. That still doesn't answer my original question though, but thanks

4. Elite Coders
Join Date
Dec 2004
Location
UK
Posts
2,358
Thanks
0
Thanked 0 Times in 0 Posts
Originally Posted by mburt
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:

Code:
`(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

5. 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"

6. Elite Coders
Join Date
Dec 2004
Location
UK
Posts
2,358
Thanks
0
Thanked 0 Times in 0 Posts
Originally Posted by mburt
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:

Code:
```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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•