q9f / eth.rb

a straightforward library to build, sign, and broadcast ethereum transactions anywhere you can run ruby.
https://q9f.github.io/eth.rb
Apache License 2.0
204 stars 88 forks source link

eth/abi: allow parsing numerics from string inputs #112

Closed q9f closed 2 years ago

q9f commented 2 years ago

fix #110

pynixwang commented 2 years ago

hex number not work

"00000000000000000000000000000000000000000000021e19e0c9bab2400000".to_i
=> 21
pynixwang commented 2 years ago

hex number not work

"00000000000000000000000000000000000000000000021e19e0c9bab2400000".to_i
=> 21
q9f commented 2 years ago

Makes sense 😮‍💨

q9f commented 2 years ago

hex number not work

"00000000000000000000000000000000000000000000021e19e0c9bab2400000".to_i
=> 21

You need to preprocess the JSON in this case. There is no way to know if a string is binary, octal, decimal, or hexadecimal.

pynixwang commented 2 years ago

decimal is enough.

I found you have hex string in test, so I have a try. do you run you test?

unixneo commented 2 years ago

I found you have hex string in test, so I have a try. do you run you test?

test = "00000000000000000000000000000000000000000000021e19e0c9bab2400000".hex
=> 10000000000000000000000
test.to_s(16)
=> "21e19e0c9bab2400000"
kurotaky commented 2 years ago

For hexadecimal numbers, the argument to_i() must be 16.

irb(main):005:0> "00000000000000000000000000000000000000000000021e19e0c9bab2400000".to_i(16)
=> 10000000000000000000000

However, it is difficult to distinguish octal, decimal, or hexadecimal in the following pattern... 😢

irb(main):016:0> '000255'.to_i(8)
=> 173
irb(main):017:0> '000255'.to_i(10)
=> 255
irb(main):018:0> '000255'.to_i(16)
=> 597
pynixwang commented 2 years ago

decimal string is enough for most cases.