Module: BinaryParser::BinaryManipulateFunction

Extended by:
BinaryManipulateFunction
Included in:
BinaryManipulateFunction
Defined in:
lib/binary_parser/general_class/binary_manipulate_function.rb

Constant Summary collapse

MASK =
[0b11111111,
0b01111111,
0b00111111,
0b00011111,
0b00001111,
0b00000111,
0b00000011,
0b00000001]

Instance Method Summary collapse

Instance Method Details

#convert_uint_into_binary(uint, bit_length) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/binary_parser/general_class/binary_manipulate_function.rb', line 38

def convert_uint_into_binary(uint, bit_length)
  if uint == 0
    if bit_length > 0
      convert_uint_into_binary(0, bit_length - 8) + [0].pack("C1")
    else
      [].pack("C0")
    end
  else
    convert_uint_into_binary(uint / 256, bit_length - 8) + [uint % 256].pack("C1")
  end
end

#convert_uint_into_binary_in_domain_of_definition?(uint, bit_length) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/binary_parser/general_class/binary_manipulate_function.rb', line 50

def convert_uint_into_binary_in_domain_of_definition?(uint, bit_length)
  bit_length >= 0 && 0 <= uint && uint < 2 ** bit_length
end

#needed_sub_string(str, bit_first_pos, bit_last_pos) ⇒ Object



14
15
16
# File 'lib/binary_parser/general_class/binary_manipulate_function.rb', line 14

def needed_sub_string(str, bit_first_pos, bit_last_pos)
  return str[(bit_first_pos / 8)..(bit_last_pos / 8)], bit_first_pos % 8, 7 - (bit_last_pos % 8)
end

#needed_sub_string_in_domain_of_definition?(str, bfp, blp) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/binary_parser/general_class/binary_manipulate_function.rb', line 18

def needed_sub_string_in_domain_of_definition?(str, bfp, blp)
  bfp < blp && blp / 8 < str.length
end

#to_unsigned_int(binary_string, margin_left = 0, margin_right = 0) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/binary_parser/general_class/binary_manipulate_function.rb', line 22

def to_unsigned_int(binary_string, margin_left=0, margin_right=0)
  chars = binary_string.unpack("C*")
  converted = chars.shift & MASK[margin_left]
  chars.each do |char|
    converted = (converted << 8) + char
  end
  return converted >> margin_right
end

#to_unsigned_int_in_domain_of_definition?(str, ml, mr) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
# File 'lib/binary_parser/general_class/binary_manipulate_function.rb', line 31

def to_unsigned_int_in_domain_of_definition?(str, ml, mr)
  [ str.length >= 1,
    0 <= ml && ml <= 7,
    0 <= mr && mr <= 7,
    str.length != 1 || ml + mr <= 7 ].all?
end