Fix for 64-bit platforms and negative numbers

This patch fixes 2 problems:
* The code only worked on 32-bit platforms (not on 64-bit)
* The code did not handle conversion of negative numbers
  correctly.