Class: Fields::HexstringField
Overview
For getting and setting via hex strings.
Instance Attribute Summary
Attributes inherited from Field
#bitstring, #default_value, #desc, #endianness, #length, #length_type, #name, #type
Instance Method Summary collapse
- #get_value ⇒ Object
-
#initialize(*args) ⇒ HexstringField
constructor
A new instance of HexstringField.
- #input_to_bitstring(value) ⇒ Object
Methods inherited from Field
#parse_buffer, #parse_buffer_lazy, #parse_buffer_strict, #randomize!, #set_raw, #set_value, #to_s
Constructor Details
#initialize(*args) ⇒ HexstringField
Returns a new instance of HexstringField.
193 194 195 196 |
# File 'lib/fields.rb', line 193 def initialize *args @length_type="variable" super end |
Instance Method Details
#get_value ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/fields.rb', line 210 def get_value return '' if @bitstring=="" unless @bitstring.length > 8 && @bitstring.length % 8 == 0 #do the best we can.. hexstring=@bitstring.to_i(2).to_s(16) (hexstring.length % 2 == 1) && hexstring="0"+hexstring else hexstring=bitstring.scan(/.{8}/).map {|e| "%.2x" % e.to_i(2)}.join end hexstring end |
#input_to_bitstring(value) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/fields.rb', line 198 def input_to_bitstring( value ) if (value.respond_to? :to_int) and value >= 0 bs=value.to_int.to_s(2) elsif (value.respond_to? :to_str) and value.to_str=~/^[a-f0-9\s]*$/ bs=value.to_str.gsub(/\s/,'').to_i(16).to_s(2) else raise ArgumentError, "HexstringField (#{@name}): Unable to parse input value." end (bs.length % 8 != 0) && bs=("0"*(8-bs.length%8) + bs) bs end |