Top Level Namespace
Defined Under Namespace
Instance Method Summary collapse
-
#base64 ⇒ Object
pull in base40 encode/decode (standard) module.
-
#hex_to_utf8(str) ⇒ Object
use hex to bin - why? why not?.
-
#is_hex?(str) ⇒ Boolean
move into call data (or keep as global helper) - why? why not? check conflict with hex/String#hex extension in hex/bytes gem??.
-
#utf8_to_hex(str) ⇒ Object
hexdata encode/decode (hex string NOT binary, utf8-encoded string) utf8-to-hex, hex-to-utf8.
Instance Method Details
#base64 ⇒ Object
pull in base40 encode/decode (standard) module
3 |
# File 'lib/calldata.rb', line 3 require 'base64' |
#hex_to_utf8(str) ⇒ Object
use hex to bin - why? why not?
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/calldata.rb', line 22 def hex_to_utf8( str ) ## use hex to bin - why? why not? # str.scan(/../).map { |x| x.hex }.pack('c*') ## fix: will NOT work for multi-byte chars ## str.scan(/../).map { |x| x.hex.chr }.join ### cut-off optionial 0x/0X str = str[2..-1] if str.start_with?( '0x' ) || str.start_with?( '0X') ## note: ethscriptios specifies that \u0000 ## (that is, \x00 - 0 byte) gets deleted/removed (even if valid utf-8) ## reason given: 0 byte in utf-8 messes up postgresql storage! [str].pack('H*').force_encoding( 'UTF-8' ).gsub( "\u0000", '' ) end |
#is_hex?(str) ⇒ Boolean
move into call data (or keep as global helper) - why? why not?
check conflict with hex/String#hex extension in hex/bytes gem??
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/calldata.rb', line 41 def is_hex?( str ) ### cut-off optionial 0x/0X str = str[2..-1] if str.start_with?( '0x' ) || str.start_with?( '0X') ## allow 0x/0X - that is zero? as valid - why? why not? return true if str.empty? hexchars_match = str.match( /\A[0-9a-fA-F]{2,}\z/) ## note: a byte requires two hexchars (hexchars must be even!! 2,4,6,8,etc.) hexchars_match && str.length.even? end |
#utf8_to_hex(str) ⇒ Object
hexdata encode/decode (hex string NOT binary, utf8-encoded string)
utf8-to-hex, hex-to-utf8
12 13 14 15 16 17 18 19 |
# File 'lib/calldata.rb', line 12 def utf8_to_hex( str ) ## use bin to hex - why? why not? # str.unpack('U'*str.length).map {|b| b.to_s(16) }.join # str.unpack('H*').first ## note: 0 or 3 must be 00 or 03 with padding!! ## or use rjust( 2, '0' ) - why? why not? # str.each_byte.map { |b| '%02x' % b }.join str.unpack('H*').first end |