Hi. This is a simple class written in Ruby, which is to encode and decode integers into their b64 equivalent.
This class serves two static methods; encode and decode. You can use this as so:Code:#B64 encoding in Ruby #You must retain this notice if you wish to use it class B64 def self.encodeInt32(value,length) stack = "" 1.step(length) { |x| offset = 6 * (length - x) val = (64 + ( value >> offset & 0x3f)) stack << val } return stack end def self.decodeInt(strVal) val = strVal intTot = 0 y = 0 x = (val.length - 1) while x >= 0 intTmp = ((val[x] - 64)) if y > 0 then intTmp = intTmp * ((64 ^ y)) end intTot += intTmp y += 1 x -= 1 end return intTot end end
Screenshot:Code:#require 'b64.rb' if you have it elsewhere puts B64.encodeInt32(12,2) puts B64.decodeInt("@L")
Enjoy!
(VL64 should be coming soon - having a bit of difficulty atm. If you want me to release encoding and decoding for the new protocol, I will.)
This is based off JoeH's class



Reply With Quote


