To go back again, repeatedly add the value and multiply by 85 until you're done. The basic algorithm is quite simple: take the remainder on division of 85 when packing into base 85, then divide and repeat, until you're done. See the Wikipedia page on Base 85 for details.) (How you deal with things that are not multiples of 4 bytes is up to you-usually the end is padded with zeros. If you're using this as an encoding scheme, your job is going to be to convert integers (4 bytes) into groups of 5 base85 numbers. (However, if this is why you should ask yourself whether it's really worth the extra hassle and whether Base 64 won't be good enough.) Return to_base(base).map, '.')īase 85 is particularly useful for ASCII encoding of binary data, which I presume is what you're using it for. Mapping ||= '0123456789abcdefghijklmnopqrstuvwxyz' Return old_to_s.bind(self).(base) unless mapping || base > 36 class Integerĭefine_method :to_s do |base=10, mapping=nil, sep=''| Again, there is no reason why it must do that, but mainly I didn't want to have to come up with a convention for returning the sign in the return value. It only works for integers, although there is no reason why it couldn't be extended to work with any arbitrary number. Raise ArgumentError, 'base must be greater than zero' unless base > 0 That was kind of an interesting question, so I went a little overboard: class Integer
0 Comments
Leave a Reply. |