class DMAPParser::Converter
The DMAPConverter class converts between binary and ruby formats
Public Class Methods
bin_to_bool(data)
click to toggle source
# File lib/dmapparser/converter.rb, line 25 def bin_to_bool(data) data == "\x01" end
bin_to_byte(data)
click to toggle source
# File lib/dmapparser/converter.rb, line 9 def bin_to_byte(data) data.unpack('C').first end
bin_to_date(data)
click to toggle source
# File lib/dmapparser/converter.rb, line 37 def bin_to_date(data) Time.at(bin_to_int(data)) end
bin_to_hex(data)
click to toggle source
# File lib/dmapparser/converter.rb, line 33 def bin_to_hex(data) data.bytes.reduce('') { |a, e| a + format('%02X', e) } end
bin_to_int(data)
click to toggle source
# File lib/dmapparser/converter.rb, line 17 def bin_to_int(data) data.unpack('N').first end
Also aliased as: bin_to_uint32
bin_to_long(data)
click to toggle source
# File lib/dmapparser/converter.rb, line 13 def bin_to_long(data) (bin_to_int(data[0..3]) << 32) | bin_to_int(data[4..7]) end
Also aliased as: bin_to_uint64
bin_to_short(data)
click to toggle source
# File lib/dmapparser/converter.rb, line 21 def bin_to_short(data) data.unpack('n').first end
Also aliased as: bin_to_uint16
bin_to_string(data)
click to toggle source
# File lib/dmapparser/converter.rb, line 89 def bin_to_string(data) data.force_encoding(Encoding::UTF_8) end
bin_to_version(data)
click to toggle source
# File lib/dmapparser/converter.rb, line 29 def bin_to_version(data) data.unpack('nCC').join '.' end
bool_to_bin(data)
click to toggle source
# File lib/dmapparser/converter.rb, line 41 def bool_to_bin(data) (data ? 1 : 0).chr end
byte_to_bin(data)
click to toggle source
# File lib/dmapparser/converter.rb, line 49 def byte_to_bin(data) [data.to_i].pack 'C' end
data_to_numeric(data)
click to toggle source
# File lib/dmapparser/converter.rb, line 77 def data_to_numeric(data) if data.bytesize == 1 return Converter.bin_to_byte(data) elsif data.bytesize == 2 return Converter.bin_to_short(data) elsif data.bytesize == 4 return Converter.bin_to_int(data) elsif data.bytesize == 8 return Converter.bin_to_long(data) end end
date_to_bin(data)
click to toggle source
# File lib/dmapparser/converter.rb, line 5 def date_to_bin(data) int_to_bin(data.to_i) end
decode(type, data)
click to toggle source
# File lib/dmapparser/converter.rb, line 106 def decode(type, data) decode_method = ('bin_to_' + type.to_s).to_sym if respond_to? decode_method send(decode_method, data) else decode_unknown(data) end end
decode_unknown(data)
click to toggle source
# File lib/dmapparser/converter.rb, line 69 def decode_unknown(data) if data =~ /[^\x20-\x7e]/ # non-readable characters data_to_numeric(data) else data end end
Also aliased as: bin_to_unknown
encode(type, data)
click to toggle source
# File lib/dmapparser/converter.rb, line 115 def encode(type, data) encode_method = (type.to_s + '_to_bin').to_sym if respond_to? encode_method send(encode_method, data) else data # This only works for strings.. end end
hex_to_bin(data)
click to toggle source
# File lib/dmapparser/converter.rb, line 65 def hex_to_bin(data) [data].pack 'H*' end
int_to_bin(data)
click to toggle source
# File lib/dmapparser/converter.rb, line 45 def int_to_bin(data) [data.to_i].pack 'N' end
Also aliased as: uint32_to_bin
long_to_bin(data)
click to toggle source
# File lib/dmapparser/converter.rb, line 53 def long_to_bin(data) [data >> 32, data & 0xFFFFFFFF].pack 'NN' end
Also aliased as: uint64_to_bin
short_to_bin(data)
click to toggle source
# File lib/dmapparser/converter.rb, line 57 def short_to_bin(data) [data.to_i].pack 'n' end
Also aliased as: uint16_to_bin
string_to_bin(data)
click to toggle source
# File lib/dmapparser/converter.rb, line 93 def string_to_bin(data) data.force_encoding(Encoding::BINARY) end
version_to_bin(data)
click to toggle source
# File lib/dmapparser/converter.rb, line 61 def version_to_bin(data) data.split('.').map(&:to_i).pack 'nCC' end