Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/tapyrus.rb
Instance Method Summary collapse
-
#bth ⇒ Object
binary convert to hex string.
-
#bti ⇒ Object
binary convert to integer.
-
#htb ⇒ Object
hex string convert to binary.
-
#opcode ⇒ Object
get opcode.
- #opcode? ⇒ Boolean
- #push_opcode? ⇒ Boolean
-
#pushdata? ⇒ Boolean
whether data push only?.
- #pushed_data ⇒ Object
-
#rhex ⇒ Object
reverse hex string endian.
-
#valid_hex? ⇒ Boolean
whether value is hex or not hex.
Instance Method Details
#bth ⇒ Object
binary convert to hex string
101 102 103 |
# File 'lib/tapyrus.rb', line 101 def bth unpack('H*').first end |
#bti ⇒ Object
binary convert to integer
111 112 113 |
# File 'lib/tapyrus.rb', line 111 def bti bth.to_i(16) end |
#htb ⇒ Object
hex string convert to binary
106 107 108 |
# File 'lib/tapyrus.rb', line 106 def htb [self].pack('H*') end |
#opcode ⇒ Object
get opcode
121 122 123 |
# File 'lib/tapyrus.rb', line 121 def opcode force_encoding(Encoding::ASCII_8BIT).ord end |
#opcode? ⇒ Boolean
125 126 127 |
# File 'lib/tapyrus.rb', line 125 def opcode? !pushdata? end |
#push_opcode? ⇒ Boolean
129 130 131 |
# File 'lib/tapyrus.rb', line 129 def push_opcode? [Tapyrus::Opcodes::OP_PUSHDATA1, Tapyrus::Opcodes::OP_PUSHDATA2, Tapyrus::Opcodes::OP_PUSHDATA4].include?(opcode) end |
#pushdata? ⇒ Boolean
whether data push only?
134 135 136 |
# File 'lib/tapyrus.rb', line 134 def pushdata? opcode <= Tapyrus::Opcodes::OP_PUSHDATA4 && opcode > Tapyrus::Opcodes::OP_0 end |
#pushed_data ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/tapyrus.rb', line 138 def pushed_data return nil unless pushdata? offset = 1 case opcode when Tapyrus::Opcodes::OP_PUSHDATA1 offset += 1 when Tapyrus::Opcodes::OP_PUSHDATA2 offset += 2 when Tapyrus::Opcodes::OP_PUSHDATA4 offset += 4 end self[offset..-1] end |
#rhex ⇒ Object
reverse hex string endian
116 117 118 |
# File 'lib/tapyrus.rb', line 116 def rhex htb.reverse.bth end |
#valid_hex? ⇒ Boolean
whether value is hex or not hex
154 155 156 |
# File 'lib/tapyrus.rb', line 154 def valid_hex? !self[/\H/] end |