2014-10-08 1 views
0

bigInteger.js 라이브러리를 사용하여 160 비트 정수로 연산을 수행하려고하지만 16 진수로 표현하고 싶습니다. 형식으로 전송하여 ID로 사용할 수 있습니다. 내가 to hexin hex와 동일하게하는 것이 필요문자열로 표시된 10 진수를 16 진수 형식으로 변환하여 여전히 문자열로 표시합니다.

in hex 4044654fce69424a651af2825b37124c25094658 
in dec 366900685503779409298642816707647664013657589336 
to hex 366900685503779409298642816707647664013657589336 

:

var git_sha1 = require('git-sha1'); 
var bigInt = require("big-integer"); 

var uuid = git_sha1((~~(Math.random() * 1e9)).toString(36) + Date.now()); 
console.log('in hex \t', uuid); // See the uuid I have 
console.log('in dec \t', bigInt(uuid, 16).toString()); // convert it to bigInt and then represent it as a string 
console.log('to hex \t', bigInt(uuid, 16).toString(16)); // try to convert it back to hex 

여기 내 출력됩니다. 어떤 제안? 고맙습니다!

+0

의 PR로 고정되었다 :(HTTPS : //github.com/peterolson/BigInteger.js/blob/master/BigInteger.js#L317-L331 의미는 'monkey patch it', dec의 문자열을 16 진수로 변환하는 방법입니다. –

+0

그 크기의 숫자에 대해서는 확실하지 않지만 다음을 시도해보십시오. http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript – CompanyDroneFromSector7G

답변

0

big-integer에 첨부되어 있는지 잘 모르겠지만, 그렇지 않은 경우 bigint은 원하는대로 정확하게 수행합니다.

편집 : 당신이 큰 정수를 유지하려면,이 트릭을 수행해야합니다

function toHexString(bigInt) { 
    var output = ''; 
    var divmod; 
    while(bigInt.notEquals(0)) { 
    divmod = bigInt.divmod(16); 
    bigInt = divmod.quotient; 
    if (divmod.remainder >= 10) 
     output = String.fromCharCode(87+divmod.remainder) + output; 
    else 
     output = divmod.remainder + output; 
    } 
    return output; 
} 
+0

당신,하지만 난 특별히 브라우저에서 실행하고 bigint가 필요합니다 libgmp –

+0

편집 : 큰 정수로 할 수있는 기능이 추가되었습니다. – ploutch

관련 문제