Module: SecurizeString::BinaryStringDataMethods::InstanceMethods
- Defined in:
- lib/securize_string/binary_string_data_methods.rb
Overview
Adds basic binary data instance methods via an include of SecurizeString::BinaryStringDataMethods.
Instance Method Summary collapse
-
#data_to_escaped_hex ⇒ Object
Returns an escaped hex string representation of the data.
-
#data_to_hex ⇒ Object
Returns the hexidecimal string representation of the data.
-
#data_to_i ⇒ Object
Returns the data converted from hexidecimal into an integer.
Instance Method Details
#data_to_escaped_hex ⇒ Object
Returns an escaped hex string representation of the data.
This hex string is compatible with Ruby and Javascript.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/securize_string/binary_string_data_methods.rb', line 62 def data_to_escaped_hex # First we convert the string into a packed hex string. hex_string = self.unpack('H*')[0] # Now we grab two elements at a time, prefix it, and add it to the buffer. ptr = 0 len = self.bytesize outbuf = "" while(ptr<(len*2)) outbuf << '\x' + hex_string[ptr,2] ptr+=2 end # Now we return the buffer return outbuf end |
#data_to_hex ⇒ Object
Returns the hexidecimal string representation of the data.
48 49 50 |
# File 'lib/securize_string/binary_string_data_methods.rb', line 48 def data_to_hex return (self.to_s.empty? ? '' : self.to_s.unpack('H*')[0]) end |
#data_to_i ⇒ Object
Returns the data converted from hexidecimal into an integer. This is usually as a BigInt.
55 56 57 |
# File 'lib/securize_string/binary_string_data_methods.rb', line 55 def data_to_i return (self.to_s.empty? ? 0 : self.data_to_hex.hex) end |