Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@alexvbush
Created June 23, 2011 23:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save alexvbush/1043912 to your computer and use it in GitHub Desktop.
Save alexvbush/1043912 to your computer and use it in GitHub Desktop.
function to convert decimal to binary in Ruby.
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