Conversor hexa, decimal y binario

Iniciado por ANTRAX, Febrero 24, 2010, 04:06:32 PM

Tema anterior - Siguiente tema

0 Miembros y 1 Visitante están viendo este tema.

Febrero 24, 2010, 04:06:32 PM Ultima modificación: Julio 31, 2014, 09:45:30 PM por Expermicid
Script que sirve de conversor entre esos 3 sistemas. Realmente este code es un caos, no tiene la claridad que suelen tener los codes de ruby pero de hecho funciona, y nos sirve para saber como se traduce matematicamente de un sistema a otro, y eso es bastante util.

Código: ruby

print "\nConversor de los sistemas numericos Hexadecimal,Binario y Decimal\n"
print "c0ded by pRotos <[email protected]>\n\n"
def uso
print "Uso: numero <entrada> <salida>\n"
print "Inicio: Sistema numerico de entrada (hex, dec, o bi)\n"
print "Destino: Sistema numerico de salida (hex, dec o bi)\n"
print "Ejemplo: 1100101 bi hex\n(devolveria el valor en hexa de ese binario)\n"
end

if ARGV[0] == nil

uso()
end

g=nil
entrada=ARGV[1]
salida=ARGV[2]
numero=ARGV[0]

case entrada
when 'dec'
when 'bi'
when 'hex'
else
g=1
if ARGV != nil
uso()
end
end


class Conv
def initialize(num, type)
@num=num
@type=type
end

def to_bi
if @type == 'bi'
return @num, "b"
elsif @type == 'hex'
[email protected]
res=Array.new
while num > 1
a=num%2
res.push(a)
num=num/2
end
res.push(1)
return res.reverse, "b"
elsif @type == 'dec'
res=Array.new
[email protected]_i
while num > 1
a=num%2
res.push(a)
num=num/2
end
res.push(1)
return res.reverse, "b"
end

end

def to_dec
if @type == 'bi'
[email protected]
[email protected]("")
c=0
b=Array.new
while l >= 0
x=(n[c].to_i)*(2**l)
b.push(x)
c+=1
l-=1
end
z=0
d=0
until z == b.length
d=d+b[z]
z+=1
end
return d
elsif @type == 'hex'
return @num.hex
elsif @type == 'dec'
return @num
end

end

def to_hex
if @type == 'hex'
return @num, "h"
elsif @type == 'dec'
[email protected]_i
res=Array.new
n=num
if n < 16
case n
when 10
n='A'

when 11
n='B'

when 12
n='C'

when 13
n='D'

when 14
n='E'

when 15
n='F'
end
return n, "h"
else
while num > 15
n=num%16
res.push(n)
num=num/16
if num < 16
j=1
case num
when 10
n='A'
res.push(n)
when 11
n='B'
res.push(n)
when 12
n='C'
res.push(n)
when 13
n='D'
res.push(n)
when 14
n='E'
res.push(n)
when 15
n='F'
res.push(n)
end


end
end
if j!=1
return "1", res, "h"
else
return res.reverse, "h"
end
end
elsif @type == 'bi'
[email protected]
[email protected]("")
c=0
b=Array.new
while l >= 0
x=(n[c].to_i)*(2**l)
b.push(x)
c+=1
l-=1
end
z=0
d=0
until z == b.length
d=d+b[z]
z+=1
end
num=d
res=Array.new
n=num
if n < 16
case n
when 10
n='A'

when 11
n='B'

when 12
n='C'

when 13
n='D'

when 14
n='E'

when 15
n='F'
end
return n, "h"
else

while num > 15
n=num%16
res.push(n)
num=num/16
if num < 16
j=1
case num
when 10
n='A'
res.push(n)
when 11
n='B'
res.push(n)
when 12
n='C'
res.push(n)
when 13
n='D'
res.push(n)
when 14
n='E'
res.push(n)
when 15
n='F'
res.push(n)
end

end
end
end
if j!=1
return "1", res, "h"
else
return res.reverse, "h"
end

end





end

end

s=Conv.new(numero, entrada)
case salida
when 'hex'
print s.to_hex, "\n\n"
when 'dec'
print s.to_dec, "\n\n"
when 'bi'
print s.to_bi, "\n\n"
else
if ARGV != nil && g==nil

uso()
end
end

###########pRUEBAS##########
#a=Conv.new('04CF', 'hex')
#b=Conv.new('3456', 'dec')
#c=Conv.new('101100110010', 'bi')
#print a.to_bi, "\n"
#print b.to_bi, "\n"
#print c.to_bi, "\n"
#print a.to_dec, "\n"
#print b.to_dec, "\n"
#print c.to_dec, "\n"
#print a.to_hex, "\n"
#print b.to_hex, "\n"
#print c.to_hex, "\n"
###########################