Created
November 18, 2011 22:18
-
-
Save amoilanen/1377953 to your computer and use it in GitHub Desktop.
Conversion from ASCII to binary codes and back
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
require "test/unit" | |
def fromBinaryCodes(text) | |
text.split(" ").map{|symbol| symbol.to_i(2).chr}.join("") | |
end | |
def toBinaryCodes(text) | |
text.split(//).map{|symbol| symbol.ord.to_s(2)}.join(" ") | |
end | |
class TestBinaryCodes < Test::Unit::TestCase | |
def test_fromBinaryCodes | |
assert_equal("abc", fromBinaryCodes("1100001 1100010 1100011")) | |
end | |
def test_toBinaryCodes | |
assert_equal("1100001 1100010 1100011", toBinaryCodes("abc")) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment