Class: Utel::Pdu
- Inherits:
-
Object
- Object
- Utel::Pdu
- Defined in:
- lib/utel/pdu.rb
Class Method Summary collapse
- .decimal_semi_octets(number) ⇒ Object
- .decode(string) ⇒ Object
-
.encode(string) ⇒ Object
Encode and decode pdu strings.
Class Method Details
.decimal_semi_octets(number) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/utel/pdu.rb', line 35 def decimal_semi_octets number reversed = '' number.to_s.split('').each_slice(2) do |pair| a,b = pair reversed << (b.nil? ? "F" : b) << a end reversed end |
.decode(string) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/utel/pdu.rb', line 18 def decode string out = '' b = 0; d = 0 string.scan(/.{2}/).each do |part| byte = part.to_i(16) c = ((byte & ((1 << 7-d)-1)) << d) | b; b = byte >> (7-d); out << c.chr d += 1 if d == 7 out << b.chr d = 0; b = 0 end end out end |
.encode(string) ⇒ Object
Encode and decode pdu strings
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/utel/pdu.rb', line 5 def encode string out = '' for i in (0..string.length - 1) do t = i%8+1 next if t == 8 c = string[i] >> i%8 b = (string[i+1] || 0) & ((1 << t)-1) c = (b << (8-t)) | c out << c.to_s(16).rjust(2, '0').upcase end out end |