Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/common.rb

Instance Method Summary collapse

Instance Method Details

#bytes_to_binObject



11
12
13
14
# File 'lib/common.rb', line 11

def bytes_to_bin
  raise "Not a byte array" unless self.is_byte_array?
  '0b' + self.map { |b| b.to_s(2).rjust(8, '0') }.join
end

#bytes_to_hexObject



6
7
8
9
# File 'lib/common.rb', line 6

def bytes_to_hex
  raise "Not a byte array" unless self.is_byte_array?
  '0x' + self.map { |b| b.to_s(16).rjust(2, '0') }.join
end

#bytes_to_utf8Object



21
22
23
24
# File 'lib/common.rb', line 21

def bytes_to_utf8
  raise "Not a byte array" unless self.is_byte_array?
  self.pack('C*').force_encoding('utf-8')
end

#is_byte_array?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/common.rb', line 26

def is_byte_array?
  self.all? {|e| e >= 0 and e <= 255 }
end