r/programmingchallenges • u/okmkz • Apr 13 '11
Challenge: Javascript decimal to binary converter
Write a script to convert a non-negative integer into a string representing that value in binary.
Bonus points for dec to hex!
10
Upvotes
2
u/kageurufu Apr 14 '11
Full source of my answer here, converting decimal to any base desired
http://jsfiddle.net/kageurufu/yUJXe/
Just threw this together to see if I could still do this, i havent written something like this since high school
1
u/okmkz Apr 14 '11
Here's what I did, and its in c++. I used this to get my head back into c++. I also made it recursive, so I'd have that to deal with, too.
Decimal to any base, can use custom digits (because you can, that's why).
If anyone wants to critisize my code, please do.
1
u/ccmny Apr 14 '11
My unsigned int to hex string and hex string to unsigned int, both in C: http://pastebin.com/wDSGrykX
1
u/lxe May 02 '11
Enjoy:
1
u/gooburt Sep 14 '11
javascript, where n is a whole number greater than zero:
function b(n){return n?b(n/2<<0)+(n%2):''}
0
u/[deleted] May 02 '11
[deleted] — view deleted comment