Created
April 3, 2012 04:58
-
-
Save bradylove/2289361 to your computer and use it in GitHub Desktop.
Simple Ruby app to convert text 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
| #!/usr/bin/env ruby | |
| # Convert strings into binary! | |
| # save as binary (without .rb) | |
| # Usage ./binary string to be converted to binary (quotes not required) | |
| class String | |
| def to_binary | |
| binary = "" | |
| self.each_byte do |b| | |
| bin_bitsize = 8 | |
| bin_bit = b.to_s(2) | |
| pad = '0' * (bin_bitsize - bin_bit.length) | |
| bin_bit = pad + bin_bit | |
| binary << bin_bit | |
| end | |
| binary | |
| end | |
| end | |
| string = ARGV[0..ARGV.size].join(" ") | |
| puts string.to_binary |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment