class Scale::Types::Bytes
Public Class Methods
decode(scale_bytes)
click to toggle source
# File lib/scale/types.rb, line 182 def self.decode(scale_bytes) puts "BEGIN " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true length = Scale::Types::Compact.decode(scale_bytes).value bytes = scale_bytes.get_next_bytes(length) # [67, 97, 102, 195, 169].pack('C*').force_encoding('utf-8') # => "Café" str = bytes.pack("C*").force_encoding("utf-8") # TODO: ? if (not str.include?("\u0000")) && str.valid_encoding? puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true new str else puts " END " + self::TYPE_NAME + ": #{scale_bytes}" if Scale::Types.debug == true new bytes.bytes_to_hex end end
Public Instance Methods
encode()
click to toggle source
# File lib/scale/types.rb, line 200 def encode if value.start_with?("0x") length = Compact.new((value.length - 2)/2).encode "#{length}#{value[2..]}" else bytes = value.unpack("C*") hex_string = bytes.bytes_to_hex[2..] length = Compact.new(bytes.length).encode "#{length}#{hex_string}" end end