Module: HexString
- Included in:
- String
- Defined in:
- lib/hex_string.rb
Overview
String extensions providing to_hex_string and to_byte_string. Requiring hex_string will automatically include HexString in the String class.
Instance Method Summary collapse
-
#to_byte_string ⇒ Object
Convert a human-readable hex string into binary data.
-
#to_hex_string(readable = true) ⇒ Object
Convert binary data into a human-readable hex string.
Instance Method Details
#to_byte_string ⇒ Object
Convert a human-readable hex string into binary data.
Assumes the target String is an ASCII, human readable sequence of hexadecimal tuples depicting a sequence of 8-bit byte values. Whitespace between tuples is allowed and will be removed before packing.
Returns a newly created string containing binary data -- the target string will not be modified.
Eg:
"68 65 6c 6c 6f".to_byte_string => "hello"
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/hex_string.rb', line 16 def to_byte_string stripped = self.gsub(/\s+/,'') unless stripped.size % 2 == 0 raise "Can't translate a string unless it has an even number of digits" end raise "Can't translate non-hex characters" if stripped =~ /[^0-9A-Fa-f]/ res = [stripped].pack('H*') if RUBY_VERSION =~ /1.8/ res else res.force_encoding("ascii-8bit") end end |
#to_hex_string(readable = true) ⇒ Object
Convert binary data into a human-readable hex string.
Whatever data is contained in the target string will be "exploded" into a sequence of hexadecimal tuples, one space between each tuple, for ease of debugging and reading.
Returns a newly created string containing the hex string -- the target binary string will not be modified.
Eg:
"hello world".to_hex_string => "68 65 6c 6c 6f 20 77 6f 72 6c 64"
40 41 42 43 44 45 46 47 |
# File 'lib/hex_string.rb', line 40 def to_hex_string(readable = true) unpacked = self.unpack('H*').first if readable unpacked.gsub(/(..)/,'\1 ').rstrip else unpacked end end |