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
103 104 105 |
# File 'lib/tapyrus.rb', line 103 def bth unpack('H*').first end |
#bti ⇒ Object
binary convert to integer
113 114 115 |
# File 'lib/tapyrus.rb', line 113 def bti bth.to_i(16) end |
#htb ⇒ Object
hex string convert to binary
108 109 110 |
# File 'lib/tapyrus.rb', line 108 def htb [self].pack('H*') end |
#opcode ⇒ Object
get opcode
123 124 125 |
# File 'lib/tapyrus.rb', line 123 def opcode force_encoding(Encoding::ASCII_8BIT).ord end |
#opcode? ⇒ Boolean
127 128 129 |
# File 'lib/tapyrus.rb', line 127 def opcode? !pushdata? end |
#push_opcode? ⇒ Boolean
131 132 133 |
# File 'lib/tapyrus.rb', line 131 def push_opcode? [Tapyrus::Opcodes::OP_PUSHDATA1, Tapyrus::Opcodes::OP_PUSHDATA2, Tapyrus::Opcodes::OP_PUSHDATA4].include?(opcode) end |
#pushdata? ⇒ Boolean
whether data push only?
136 137 138 |
# File 'lib/tapyrus.rb', line 136 def pushdata? opcode <= Tapyrus::Opcodes::OP_PUSHDATA4 && opcode > Tapyrus::Opcodes::OP_0 end |
#pushed_data ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/tapyrus.rb', line 140 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
118 119 120 |
# File 'lib/tapyrus.rb', line 118 def rhex htb.reverse.bth end |
#valid_hex? ⇒ Boolean
whether value is hex or not hex
156 157 158 |
# File 'lib/tapyrus.rb', line 156 def valid_hex? !self[/\H/] end |