Created
February 27, 2011 21:03
-
-
Save xadn/846537 to your computer and use it in GitHub Desktop.
Ruby zero pad a number and convert it to binary
This file contains hidden or 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
# add leading zeros to a number | |
def zero_pad(target_length, input_string) | |
pad_length = target_length - input_string.length | |
"0" * pad_length + input | |
end | |
# convert a number to binary | |
address.to_i.to_s(2) | |
#to_i converts the string to an integer | |
#to_s(2) converts the integer in base 2 to a string | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There are built-in solution for this: