16

I need to find a way to convert a large number into a hex string in javascript. Straight off the bat, I tried myBigNumber.toString(16) but if myBigNumber has a very large value (eg 1298925419114529174706173) then myBigNumber.toString(16) will return an erroneous result, which is just brilliant. I tried writing by own function as follows:

function (integer) {
    var result = '';

    while (integer) {
        result = (integer % 16).toString(16) + result;
        integer = Math.floor(integer / 16);
    }
}

However, large numbers modulo 16 all return 0 (I think this fundamental issue is what is causing the problem with toString. I also tried replacing (integer % 16) with (integer - 16 * Math.floor(integer/16)) but that had the same issue.

I have also looked at the Big Integer Javascript library but that is a huge plugin for one, hopefully relatively straightforward problem.

Any thoughts as to how I can get a valid result? Maybe some sort of divide and conquer approach? I am really rather stuck here.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Joshua Bambrick
  • 2,669
  • 5
  • 27
  • 35
  • 1
    This is a problem with big numbers, not converting to _String_ `1298925419114529174706173 === 1298925419114529174706170` – Paul S. Sep 05 '13 at 03:20
  • Your number is way over the javascript largest integer, see http://stackoverflow.com/questions/307179/what-is-javascripts-max-int-whats-the-highest-integer-value-a-number-can-go-t – bfavaretto Sep 05 '13 at 03:20
  • 2
    well that's no fun. If I had a base 10 representation of the number as a string, is there a straightforward way to convert that to a hex string? – Joshua Bambrick Sep 05 '13 at 03:24
  • I found http://danvk.org/hex2dec.html which provides a very straightforward way of achieving this. Thanks for your help – Joshua Bambrick Sep 05 '13 at 03:28

3 Answers3

14

Assuming you have your integer stored as a decimal string like '1298925419114529174706173':

function dec2hex(str){ // .toString(16) only works up to 2^53
    var dec = str.toString().split(''), sum = [], hex = [], i, s
    while(dec.length){
        s = 1 * dec.shift()
        for(i = 0; s || i < sum.length; i++){
            s += (sum[i] || 0) * 10
            sum[i] = s % 16
            s = (s - sum[i]) / 16
        }
    }
    while(sum.length){
        hex.push(sum.pop().toString(16))
    }
    return hex.join('')
}
Collin Anderson
  • 14,787
  • 6
  • 68
  • 57
4

The numbers in question are above javascript's largest integer. However, you can work with such large numbers by strings and there are some plugins which can help you do this. An example which is particularly useful in this circumstance is hex2dec

Joshua Bambrick
  • 2,669
  • 5
  • 27
  • 35
2

The approach I took was to use the bignumber.js library and create a BigNumber passing in the value as a string then just use toString to convert to hex:

  const BigNumber = require('bignumber.js');
  const lrgIntStr = '1298925419114529174706173';
  const bn = new BigNumber(lrgIntStr);
  const hex = bn.toString(16);
Robert Brisita
  • 5,461
  • 3
  • 36
  • 35