Class: Bytes
Constant Summary
Constants included
from BytesHelper
BytesHelper::HEX_RE
Class Method Summary
collapse
Instance Method Summary
collapse
bin_to_hex, hex_to_bin, is_hex?
Constructor Details
#initialize(bin = String.new) ⇒ Bytes
Returns a new instance of Bytes.
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/bytes.rb', line 65
def initialize( bin=String.new )
@bin = if bin.encoding != Encoding::ASCII_8BIT
puts "!! WARN - Bytes.new - BINARY/ASCII-8BIT encoding expected; got: #{bin.encoding} for string:"
pp bin
bin.b
else
bin
end
end
|
Class Method Details
.convert(arg) ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/bytes.rb', line 99
def self.convert( arg )
if arg.is_a?( Bytes )
arg elsif arg.is_a?( String )
if arg.encoding == Encoding::ASCII_8BIT
Bytes.wrap( arg ) else Bytes.from_hex( arg )
end
else
raise ArgumentError, "Bytes() expected String; got #{arg.class.name}"
end
end
|
.from_hex(hex) ⇒ Object
59
|
# File 'lib/bytes.rb', line 59
def self.from_hex( hex ) new( hex_to_bin( hex ) ); end
|
.wrap(bin) ⇒ Object
“semantic” constructor for wrapping (binary) strings e.g. same as Bytes.new(bin)
62
|
# File 'lib/bytes.rb', line 62
def self.wrap( bin ) new( bin ); end
|
Instance Method Details
#==(other) ⇒ Object
93
94
95
|
# File 'lib/bytes.rb', line 93
def ==(other)
self.class == other.class && @bin == other.b
end
|
#b ⇒ Object
88
|
# File 'lib/bytes.rb', line 88
def b() @bin; end
|
#to_hex ⇒ Object
Also known as:
to_s
85
|
# File 'lib/bytes.rb', line 85
def to_hex() Bytes.bin_to_hex( @bin ); end
|