Skip to content

Instantly share code, notes, and snippets.

@ackintosh
Created December 3, 2013 14:49
Show Gist options
  • Select an option

  • Save ackintosh/7770360 to your computer and use it in GitHub Desktop.

Select an option

Save ackintosh/7770360 to your computer and use it in GitHub Desktop.
VBCode in Ruby.
class Fixnum
def encode
n = self
bytes = []
loop do
bytes.unshift n % 128
break if n < 128
n = n / 128
end
bytes[-1] += 128
bytes.pack("C*")
end
end
class String
def decode
numbers = []
n = 0
self.unpack("C*").each do |i|
if i < 128
n = 128 * n + i
else
n = 128 * n + (i - 128)
numbers << n
n = 0
end
end
numbers[0]
end
end
if $0 == __FILE__
d = 256.encode
p d
p d.decode
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment