0

From the document toBN is safer than new BN. However, given the same hex hash string, it results in something different:

(new web3.utils.BN('0x149e7b36b314b81fbe1762060fcae0f43be82f1251166a61fc7669e7c236c4bf')).toString()
// results in: '331504813713151826241762061631415442483512511670626276704822372525'

web3.utils.toBN('0x149e7b36b314b81fbe1762060fcae0f43be82f1251166a61fc7669e7c236c4bf').toString()
// results in: '9326269197397511364330713577831447135626556432227477146325167069213775807679'

Now I'm wondering which one should I use for hex string?

EDIT

For non-hex string, it creates the same value:

(new web3.utils.BN('57896044618658097711785492504343953926634992332820282019728792003956564819968')).toString()
// results in: '57896044618658097711785492504343953926634992332820282019728792003956564819968'

web3.utils.toBN('57896044618658097711785492504343953926634992332820282019728792003956564819968').toString()
// results in: '57896044618658097711785492504343953926634992332820282019728792003956564819968'
daisy
  • 22,498
  • 29
  • 129
  • 265

1 Answers1

0
  • web3.utils.BN interprets the "ascii" string as if each character belongs to a number in an arbitrary base following the hex char notation 0xa = 10 and so on (I'm unsure about if someone is uses that) : (new web3.utils.BN('x')).toString() -> '33' .

  • on the other hand, web3.utils.toBN is giving the expected result.

tenuki
  • 73
  • 5