Created
June 23, 2011 23:44
-
-
Save alexvbush/1043912 to your computer and use it in GitHub Desktop.
function to convert decimal to binary in Ruby.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dec2bin(number) | |
number = Integer(number) | |
if(number == 0) then 0 end | |
ret_bin = "" | |
while(number != 0) | |
ret_bin = String(number % 2) + ret_bin | |
number = number / 2 | |
end | |
ret_bin | |
end | |
# Integer("0b#{dec2bin 5}") will convert back to decimal. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment