Advertisement
AngryPacman

Base Conversion

Mar 12th, 2012
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.27 KB | None | 0 0
  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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement