रूबी में बेस से आने और कोशिश करने के लिए ... मैंने जो कोड डाला है, वह काफी दोहराया गया है। क्या एक बेहतर तरीका है?हेक्साडेसिमल, दशमलव, ऑक्टल, और ASCII को परिवर्तित करना?
module Converter
def self.convert(value, from, to)
case from
when :hex
case to
when :dec
# code to change hex to dec
when :oct
# code to change hex to oct
when :bin
# code to change hex to bin
when :ascii
# code to change hex to ascii
end
when :dec
case to
when :hex
# code to change dec to hex
when :oct
# code to change dec to oct
when :bin
# code to change dec to bin
when :ascii
# code to change dec to ascii
end
when :oct
case to
when :hex
# code to change oct to hex
when :dec
# code to change oct to dec
when :bin
# code to change oct to bin
when :ascii
# code to change oct to ascii
end
when :bin
case to
when :hex
# code to change bin to hex
when :dec
# code to change bin to dec
when :oct
# code to change bin to oct
when :ascii
# code to change bin to ascii
end
when :ascii
case to
when :hex
# code to change ascii to hex
when :dec
# code to change ascii to dec
when :oct
# code to change ascii to oct
when :bin
# code to change ascii to bin
end
end
end
end
कनवर्ट स्ट्रिंग और स्ट्रिंग स्ट्रिंग लेता है, है ना? क्या आप कुछ उदाहरण दिखा सकते हैं? हेक्साडेसिमल उदाहरण के लिए "0xXX" हैं? – tokland