Underc0de

Programación Scripting => Ruby => Mensaje iniciado por: B3N en Junio 20, 2015, 10:33:08 pm

Título: Convertidor de string a binario y viceversa
Publicado por: B3N en Junio 20, 2015, 10:33:08 pm
Pueden sugerir arreglos. Espero les sea de utilidad.

Código: (ruby) [Seleccionar]
# Copyright (C) 2015 B3N <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Description: String to binary converter and viceversa
# Original name: bconverter.rb
# Version: 1.0

#!/usr/bin/env ruby

def help
puts
puts "Usage: #{__FILE__} -s \"String to convert to binary\""
puts "       #{__FILE__} -b \"00111011 00101001\""
end

if ARGV.empty? || ARGV.length != 2
puts help
elsif ARGV[0] == '-s'
ARGV[1].unpack("C*").each{|c| print "%08b " % c}
print "\n"
elsif ARGV[0] == '-b'
puts [ARGV[1].delete(' ')].pack("B*")
else
puts
puts "[-] incorrect option: #{ARGV[0]}"
help
puts
end