Dec to Hex number conversion with toString(16) gives wrong results

Vaidas Bat
Kilo Expert

Hi everyone, I facing an issue when I'm dealing with transform map step in the discovery pattern.

Long story short while using EVAL() function to change decimal number that I getting from discovery to hex with formula NUMBER.toString(16) bu it gives me value which is not correct

Here is what inside eval function:

_________________________code start__________________________________

var rtrn='' ";
var number= ${Ctable_merged_final[].number};
var hex = (+aviserial).toString(16);


if (number% 1 === 0) {
rtrn = (hex);
}
else {
rtrn = number;
}

_________________________code end__________________________________

Example :

Here var number = 1850962752024732466832303299683338850660427134

I want that if condition met answer would be rtrn = 530002A57EFBDA7DEC6F9B384E00000002A57E

But after using ".toString(16)" forumla, answer is rtrn = 530002a57efbdc000000000000000000000000

it seems that only half of number is being translated to hex and another half changed as c+zeros....

Can anyone suggest any trick or tip on this?

 

 

1 ACCEPTED SOLUTION

Vaidas Bat
Kilo Expert

Found the answer myself:

This is javascript issue, assuming you have your integer stored as a decimal string which is in my case is to long for javascript. Therefore I used this function to get my dec converted to hex correctly:

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('')
}


Source - https://stackoverflow.com/questions/18626844/convert-a-large-integer-to-a-hex-string-in-javascript

View solution in original post

2 REPLIES 2

Vaidas Bat
Kilo Expert

Just noticed that this line:

var hex = (+aviserial).toString(16);

supposed to be:

var hex = (+number).toString(16);

But this is not the point, the point is that function .toString(16) doesnt give "full" results

Vaidas Bat
Kilo Expert

Found the answer myself:

This is javascript issue, assuming you have your integer stored as a decimal string which is in my case is to long for javascript. Therefore I used this function to get my dec converted to hex correctly:

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('')
}


Source - https://stackoverflow.com/questions/18626844/convert-a-large-integer-to-a-hex-string-in-javascript