# File lib/ascii85.rb, line 37
  def self.encode(str, wrap_lines = 80)

    return '' if str.to_s.empty?

    # Compute number of \0s to pad the message with (0..3)
    padding_length = (-str.to_s.length) % 4

    # Extract big-endian integers
    tuples = (str.to_s + ("\0" * padding_length)).unpack('N*')

    # Encode
    tuples.map! do |tuple|
      if tuple == 0
        'z'
      else
        tmp = ""
        5.times do
          tmp += ((tuple % 85) + 33).chr
          tuple /= 85
        end
        tmp.reverse
      end
    end

    # We can't use the z-abbreviation if we're going to cut off padding
    if (padding_length > 0) and (tuples.last == 'z')
      tuples[-1] = '!!!!!'
    end

    # Cut off the padding
    tuples[-1] = tuples[-1][0..(4 - padding_length)]

    # Add start-marker and join into a String
    result = '<~' + tuples.join

    # If we don't need to wrap the lines to a certain length, add ~> and return
    if (!wrap_lines)
      return result + '~>'
    end

    # Otherwise we wrap the lines

    line_length = [2, wrap_lines.to_i].max

    wrapped = []
    0.step(result.length, line_length) do |index|
      wrapped << result.slice(index, line_length)
    end

    # Add end-marker -- on a new line if necessary
    if (wrapped.last.length + 2) > line_length
      wrapped << '~>'
    else
      wrapped[-1] += '~>'
    end

    return wrapped.join("\n")
  end