Created
April 9, 2015 02:31
-
-
Save jish/7178956598bfff909731 to your computer and use it in GitHub Desktop.
hex 2 base64
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
input = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d" | |
expected_output = "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t" | |
def six(string) | |
string.scan(/\w{6}/) | |
end | |
def to_binary(hex_string) | |
expected_length = (hex_string.length / 2) * 8 | |
"%0#{expected_length}d" % hex_string.to_i(16).to_s(2) | |
end | |
chunks = six(input) | |
characters = (("A".."Z").to_a + ("a".."z").to_a + (0..9).to_a + ["?"] + ["?"]) | |
retval = chunks.map do |chunk| | |
binary = to_binary(chunk) | |
puts [chunk, binary].join(" => ") | |
bits = six(binary) | |
in64 = bits.map do |b| | |
decimal = b.to_i(2) | |
c = characters[decimal] | |
puts [b, decimal, c].join(" => ") | |
c | |
end | |
end | |
puts retval.join |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment