Skip to main content
added 225 characters in body
Source Link
ETHproductions
  • 50.3k
  • 6
  • 96
  • 241

Converting a number from hexadecimal

Instead of using the lengthy parseInt to convert a number from hexadecimal:

parseInt(a,16) 

Add "0x" to the beginning, then convert to a number with a usual technique:

+("0x"+a) // 5 bytes saved 

Even better solution, abusing order of operations:

"0x"+a-0 // another byte saved 

Note that this last one will not work in all situations, depending on the surrounding operators.

In ES6, you can also use this trick to convert from octal or binary:

"0b"+a-0 // binary "0o"+a-0 // octal 

Converting a number from hexadecimal

Instead of using the lengthy parseInt to convert a number from hexadecimal:

parseInt(a,16) 

Add "0x" to the beginning, then convert to a number with a usual technique:

+("0x"+a) // 5 bytes saved 

Even better solution, abusing order of operations:

"0x"+a-0 // another byte saved 

Converting a number from hexadecimal

Instead of using the lengthy parseInt to convert a number from hexadecimal:

parseInt(a,16) 

Add "0x" to the beginning, then convert to a number with a usual technique:

+("0x"+a) // 5 bytes saved 

Even better solution, abusing order of operations:

"0x"+a-0 // another byte saved 

Note that this last one will not work in all situations, depending on the surrounding operators.

In ES6, you can also use this trick to convert from octal or binary:

"0b"+a-0 // binary "0o"+a-0 // octal 
Source Link
ETHproductions
  • 50.3k
  • 6
  • 96
  • 241

Converting a number from hexadecimal

Instead of using the lengthy parseInt to convert a number from hexadecimal:

parseInt(a,16) 

Add "0x" to the beginning, then convert to a number with a usual technique:

+("0x"+a) // 5 bytes saved 

Even better solution, abusing order of operations:

"0x"+a-0 // another byte saved