When I want to convert a binary string to a base 10 decimal (Like this: parseInt('0001010', 2)
), Javascript returns a decimal number, but a version in which the leading zeros mentioned in the example above have been disregarded. Is there any way to fix this?
Asked
Active
Viewed 166 times
0

jona
- 399
- 4
- 14
-
?? The result of that example should be the number 10. What did you expect instead? – Pointy Dec 27 '14 at 18:37
-
@Pointy what did he get, actually, the result of that number is ten – Dec 27 '14 at 18:38
-
@NPE ha ha not enough/too much coffee :) :) – Pointy Dec 27 '14 at 18:39
-
@NPE ten. I've just run it on chrome console – Dec 27 '14 at 18:39
-
See, if I try to parse `0001010011111101001010010`, it gives me 2751058, which, when reparsed through `str.toString(2)`, gives me `1010011111101001010010`. – jona Dec 27 '14 at 18:39
-
1when going back to the original number it does not add the 0's at the left side. – Dec 27 '14 at 18:40
-
huh. Then how exactly would I achieve that it does that? – jona Dec 27 '14 at 18:41
-
Why do you want to do so? If you need a fixed length of number, better count the number of digits and add the 0's on the left side instead. – Dec 27 '14 at 18:42
-
@JuanRocamonde But what if I want to sort of shorten a long binary number, while being able to restore it to its original form later? – jona Dec 27 '14 at 18:43
-
1Store the length in a variable and then, once restored, add the necessary number of 0's. – Dec 27 '14 at 18:44
-
@kyr This is a problem of presentation. In base-10, we wouldn't write "four hundred" as 00000000400. Just pad with the number of zeroes you want in your UI / output. – Yuck Dec 27 '14 at 18:44
-
The problem I have is that I need to somehow fit everything in a URL query. – jona Dec 27 '14 at 18:45
-
@kyr check my answer. It might be helpful – Dec 27 '14 at 18:49
-
Yeah, I figured so. Thanks, I'll accept that answer. – jona Dec 27 '14 at 18:50
2 Answers
1
The decimal representation has no way to keep track of leading zeros. If you wish to keep the leading zeros in the result, you need a fundamentally different approach (e.g. keeping the output as a string).
Alternatively, if you know the width of the result a priori, you could just pad it with leading zeros on output.

NPE
- 486,780
- 108
- 951
- 1,012
1
So supposing you have the number '00000101101101':
var number = '00000101101101';
var length = number.length;
var decimal_number = parseInt(number, 2);
// going back
var new_number = decimal_number.toString(2);
var new_length = new_number.length;
var n_zeros = length - new_length;
var zeros = (n_zeros >= 2 ? Array(n_zeros+1).join("0") : "0");
new_number = zeros + new_number;
-
2This is the right idea, but in JavaScript `0*n_zeros` doesn't do what the code expects it to do. [See this old question.](http://stackoverflow.com/questions/10073699/pad-a-number-with-leading-zeros-in-javascript) – Pointy Dec 27 '14 at 18:51
-
-
1No, it doesn't; `*` is always numeric multiplication. See the link I added to the first comment. – Pointy Dec 27 '14 at 18:52
-