RyanScottLewis
RyanScottLewis

Reputation: 14016

Converting Hexadecimal, Decimal, Octal, and ASCII?

Trying to and from bases in Ruby... the code I've laid out seems quite repetitious. Is ther a better way?

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

Upvotes: 23

Views: 27241

Answers (3)

steenslag
steenslag

Reputation: 80065

class String
  def convert_base(from, to)
    self.to_i(from).to_s(to)
    # works up-to base 36
  end
end

p '1010'.convert_base(2, 10) #=> "10"
p 'FF'.convert_base(16, 2)   #=> "11111111"

Upvotes: 80

Douglas G. Allen
Douglas G. Allen

Reputation: 2261

I do not agree with using String class to manipulate binary data. It would seem more appropriate to use Fixnum as there are bitwise operators in that class. Granted, the String class has the String#to_s with "ENV" and will change an Integer to a new base, 10.to_s(16) we are working with numbers here. But that is just IMHO. Good answer otherwise.

Here's my usage examples for Fixnum.

class Fixnum
  def convert_base(to)
    self.to_s(to).to_i
  end
end

p '1010'.to_i(2).convert_base(10) #=> 10  real numbers
p 'FF'.hex.convert_base(2)        #=> 11111111
p 72.convert_base(16)             #=> 48

Upvotes: 2

Matti Virkkunen
Matti Virkkunen

Reputation: 65126

Come up with code to convert from anything to decimal and from decimal to anything and then combine those. E.g. to convert from binary to hex, convert to decimal first and then that to hex. Base conversion is also trivial to implement in a generic way that can handle any base, given the set of digits it uses.

Also, please remember that a numeric value in memory doesn't really have the concept of a base (it's represented as binary, obviously, but that's mostly irrelevant). It's just a value. Only once you get strings involved do bases become really significant. So if your "decimal" really means a numeric value instead of a string of digits, it might be best to not call it "decimal".

Upvotes: 3

Related Questions