SHARE
TWEET

Base Conversion

AngryPacman Mar 12th, 2012 154 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class String
  2.   def convert_base(from, to)
  3.     if from == 1
  4.       return self.size.to_s(to)
  5.     end
  6.     if to == 1
  7.       n = self.to_i(from).to_s(10).dup
  8.       s = ''
  9.       n.to_i.times do s += '1' end
  10.       return s
  11.     end
  12.     self.to_i(from).to_s(to)
  13.   end
  14. end
RAW Paste Data
Top