{"id":9334,"date":"2012-12-30T21:08:05","date_gmt":"2012-12-30T15:38:05","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=9334"},"modified":"2014-06-22T23:15:25","modified_gmt":"2014-06-22T17:45:25","slug":"javascript-hex-string-to-int64-conversion-similar-to-int64-parse","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/javascript-hex-string-to-int64-conversion-similar-to-int64-parse\/","title":{"rendered":"JavaScript: Hex String To Int64 conversion (similar to Int64.parse)"},"content":{"rendered":"<p>Recently in our Node.js application we had a task where we needed to convert the Windows hexadecimal Live Id (lid) to sighed 64 bit Integer such that it can be used in the API calls. But there was no direct way to convert that in JavaScript, but here i will discuss the workaround we took.<\/p>\n<p>&nbsp;<\/p>\n<h1>The problem with 64 bit Integer in JavaScript<\/h1>\n<p>Though javaScript numbers are 64 bit but <strong>they only use 53 bits to store the real value(mantissa)<\/strong>, rest bits are used for exponent. Hence this makes <strong>holding of 64bit integer impossible in JavaScript<\/strong>.  <\/p>\n<p>&nbsp;<\/p>\n<h1>The workaround Solution<\/h1>\n<p>The workaround is to use String\/Arrays to hold large numbers and perform arithmetic operations on that. The final result has to be saved in a string as we can never save it as number data type. I know it is dirty, but that&#8217;s the only way.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong>Example:<\/strong> Converting hex windows live id to Integer Live Id (<strong>Signed<\/strong> 64bit Integer)<\/h4>\n<p>Following code will convert the received hex lid to intLid:<br \/>\n[javascript]<br \/>\nvar lidHex = &quot;b4fb0acfc47086c1&quot;; \/\/The Input Hex Number.<br \/>\nvar lid64 = new HexStringToInt64StringConverter(true).convert(lidHex);  \/\/&quot;true&quot; is passed for Signed Conversion.<br \/>\nconsole.log(lid64); \/\/ output: -5405715040257931583<br \/>\n[\/javascript]<br \/>\n<strong>Note:<\/strong> Implementation of <strong>HexStringToInt64StringConverter<\/strong> is discussed below.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong>Example:<\/strong> Converting hex number to Integer (<strong>Unsigned<\/strong> 64bit Integer)<\/h4>\n<p>Following code will convert the received hex lid to intLid:<br \/>\n[javascript]<br \/>\nvar intHex = &quot;b4fb0acfc47086c1&quot;; \/\/The Input Hex Number.<br \/>\nvar int64 = new HexStringToInt64StringConverter(false).convert(intHex);  \/\/&quot;false&quot; is passed for UnSigned Conversion.<br \/>\nconsole.log(int64); \/\/ output: 13041029033451620033<br \/>\n[\/javascript]<br \/>\n<strong>Note:<\/strong> Implementation of <strong>HexStringToInt64StringConverter<\/strong> is discussed below.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong>Implementation:<\/strong> HexStringToInt64StringConverter <\/h4>\n<p>This converter is implemented with following points in mind:<\/p>\n<ol>\n<li>2&#8217;s Complement Signed Representation<\/li>\n<li>Number represented in Array for arithmetic manipulations.<\/li>\n<li>All Powers of 2 which are impossible to calculate in JavaScript are pre-calculated and hard coded for faster conversions.<\/li>\n<li>Output number to be represented as a string.<\/li>\n<li>Both signed and unsigned conversions.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>[javascript]<br \/>\nfunction HexStringToInt64StringConverter(signed) {<br \/>\n    var hexCode = {<br \/>\n        &#8216;0&#8217;:&quot;0000&quot;,<br \/>\n        &#8216;1&#8217;:&quot;0001&quot;,<br \/>\n        &#8216;2&#8217;:&quot;0010&quot;,<br \/>\n        &#8216;3&#8217;:&quot;0011&quot;,<br \/>\n        &#8216;4&#8217;:&quot;0100&quot;,<br \/>\n        &#8216;5&#8217;:&quot;0101&quot;,<br \/>\n        &#8216;6&#8217;:&quot;0110&quot;,<br \/>\n        &#8216;7&#8217;:&quot;0111&quot;,<br \/>\n        &#8216;8&#8217;:&quot;1000&quot;,<br \/>\n        &#8216;9&#8217;:&quot;1001&quot;,<br \/>\n        &#8216;a&#8217;:&quot;1010&quot;,<br \/>\n        &#8216;b&#8217;:&quot;1011&quot;,<br \/>\n        &#8216;c&#8217;:&quot;1100&quot;,<br \/>\n        &#8216;d&#8217;:&quot;1101&quot;,<br \/>\n        &#8216;e&#8217;:&quot;1110&quot;,<br \/>\n        &#8216;f&#8217;:&quot;1111&quot;<br \/>\n    };<br \/>\n    var preComputedLongMath = {<br \/>\n        &quot;20&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],<br \/>\n        &quot;21&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],<br \/>\n        &quot;22&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4],<br \/>\n        &quot;23&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8],<br \/>\n        &quot;24&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6],<br \/>\n        &quot;25&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2],<br \/>\n        &quot;26&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 4],<br \/>\n        &quot;27&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 8],<br \/>\n        &quot;28&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 5, 6],<br \/>\n        &quot;29&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 1, 2],<br \/>\n        &quot;210&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 4],<br \/>\n        &quot;211&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 4, 8],<br \/>\n        &quot;212&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 9, 6],<br \/>\n        &quot;213&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 1, 9, 2],<br \/>\n        &quot;214&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 3, 8, 4],<br \/>\n        &quot;215&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 7, 6, 8],<br \/>\n        &quot;216&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 5, 5, 3, 6],<br \/>\n        &quot;217&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 0, 7, 2],<br \/>\n        &quot;218&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 6, 2, 1, 4, 4],<br \/>\n        &quot;219&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 2, 4, 2, 8, 8],<br \/>\n        &quot;220&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 4, 8, 5, 7, 6],<br \/>\n        &quot;221&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 9, 7, 1, 5, 2],<br \/>\n        &quot;222&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 1, 9, 4, 3, 0, 4],<br \/>\n        &quot;223&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 3, 8, 8, 6, 0, 8],<br \/>\n        &quot;224&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 7, 7, 7, 2, 1, 6],<br \/>\n        &quot;225&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 5, 5, 4, 4, 3, 2],<br \/>\n        &quot;226&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 1, 0, 8, 8, 6, 4],<br \/>\n        &quot;227&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 4, 2, 1, 7, 7, 2, 8],<br \/>\n        &quot;228&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 6, 8, 4, 3, 5, 4, 5, 6],<br \/>\n        &quot;229&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 3, 6, 8, 7, 0, 9, 1, 2],<br \/>\n        &quot;230&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 7, 3, 7, 4, 1, 8, 2, 4],<br \/>\n        &quot;231&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 4, 7, 4, 8, 3, 6, 4, 8],<br \/>\n        &quot;232&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 9, 4, 9, 6, 7, 2, 9, 6],<br \/>\n        &quot;233&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 5, 8, 9, 9, 3, 4, 5, 9, 2],<br \/>\n        &quot;234&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 7, 1, 7, 9, 8, 6, 9, 1, 8, 4],<br \/>\n        &quot;235&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 3, 5, 9, 7, 3, 8, 3, 6, 8],<br \/>\n        &quot;236&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 8, 7, 1, 9, 4, 7, 6, 7, 3, 6],<br \/>\n        &quot;237&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 7, 4, 3, 8, 9, 5, 3, 4, 7, 2],<br \/>\n        &quot;238&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 2, 7, 4, 8, 7, 7, 9, 0, 6, 9, 4, 4],<br \/>\n        &quot;239&quot;:[0, 0, 0, 0, 0, 0, 0, 0, 5, 4, 9, 7, 5, 5, 8, 1, 3, 8, 8, 8],<br \/>\n        &quot;240&quot;:[0, 0, 0, 0, 0, 0, 0, 1, 0, 9, 9, 5, 1, 1, 6, 2, 7, 7, 7, 6],<br \/>\n        &quot;241&quot;:[0, 0, 0, 0, 0, 0, 0, 2, 1, 9, 9, 0, 2, 3, 2, 5, 5, 5, 5, 2],<br \/>\n        &quot;242&quot;:[0, 0, 0, 0, 0, 0, 0, 4, 3, 9, 8, 0, 4, 6, 5, 1, 1, 1, 0, 4],<br \/>\n        &quot;243&quot;:[0, 0, 0, 0, 0, 0, 0, 8, 7, 9, 6, 0, 9, 3, 0, 2, 2, 2, 0, 8],<br \/>\n        &quot;244&quot;:[0, 0, 0, 0, 0, 0, 1, 7, 5, 9, 2, 1, 8, 6, 0, 4, 4, 4, 1, 6],<br \/>\n        &quot;245&quot;:[0, 0, 0, 0, 0, 0, 3, 5, 1, 8, 4, 3, 7, 2, 0, 8, 8, 8, 3, 2],<br \/>\n        &quot;246&quot;:[0, 0, 0, 0, 0, 0, 7, 0, 3, 6, 8, 7, 4, 4, 1, 7, 7, 6, 6, 4],<br \/>\n        &quot;247&quot;:[0, 0, 0, 0, 0, 1, 4, 0, 7, 3, 7, 4, 8, 8, 3, 5, 5, 3, 2, 8],<br \/>\n        &quot;248&quot;:[0, 0, 0, 0, 0, 2, 8, 1, 4, 7, 4, 9, 7, 6, 7, 1, 0, 6, 5, 6],<br \/>\n        &quot;249&quot;:[0, 0, 0, 0, 0, 5, 6, 2, 9, 4, 9, 9, 5, 3, 4, 2, 1, 3, 1, 2],<br \/>\n        &quot;250&quot;:[0, 0, 0, 0, 1, 1, 2, 5, 8, 9, 9, 9, 0, 6, 8, 4, 2, 6, 2, 4],<br \/>\n        &quot;251&quot;:[0, 0, 0, 0, 2, 2, 5, 1, 7, 9, 9, 8, 1, 3, 6, 8, 5, 2, 4, 8],<br \/>\n        &quot;252&quot;:[0, 0, 0, 0, 4, 5, 0, 3, 5, 9, 9, 6, 2, 7, 3, 7, 0, 4, 9, 6],<br \/>\n        &quot;253&quot;:[0, 0, 0, 0, 9, 0, 0, 7, 1, 9, 9, 2, 5, 4, 7, 4, 0, 9, 9, 2],<br \/>\n        &quot;254&quot;:[0, 0, 0, 1, 8, 0, 1, 4, 3, 9, 8, 5, 0, 9, 4, 8, 1, 9, 8, 4],<br \/>\n        &quot;255&quot;:[0, 0, 0, 3, 6, 0, 2, 8, 7, 9, 7, 0, 1, 8, 9, 6, 3, 9, 6, 8],<br \/>\n        &quot;256&quot;:[0, 0, 0, 7, 2, 0, 5, 7, 5, 9, 4, 0, 3, 7, 9, 2, 7, 9, 3, 6],<br \/>\n        &quot;257&quot;:[0, 0, 1, 4, 4, 1, 1, 5, 1, 8, 8, 0, 7, 5, 8, 5, 5, 8, 7, 2],<br \/>\n        &quot;258&quot;:[0, 0, 2, 8, 8, 2, 3, 0, 3, 7, 6, 1, 5, 1, 7, 1, 1, 7, 4, 4],<br \/>\n        &quot;259&quot;:[0, 0, 5, 7, 6, 4, 6, 0, 7, 5, 2, 3, 0, 3, 4, 2, 3, 4, 8, 8],<br \/>\n        &quot;260&quot;:[0, 1, 1, 5, 2, 9, 2, 1, 5, 0, 4, 6, 0, 6, 8, 4, 6, 9, 7, 6],<br \/>\n        &quot;261&quot;:[0, 2, 3, 0, 5, 8, 4, 3, 0, 0, 9, 2, 1, 3, 6, 9, 3, 9, 5, 2],<br \/>\n        &quot;262&quot;:[0, 4, 6, 1, 1, 6, 8, 6, 0, 1, 8, 4, 2, 7, 3, 8, 7, 9, 0, 4],<br \/>\n        &quot;263&quot;:[0, 9, 2, 2, 3, 3, 7, 2, 0, 3, 6, 8, 5, 4, 7, 7, 5, 8, 0, 8],<br \/>\n        &quot;264&quot;:[1, 8, 4, 4, 6, 7, 4, 4, 0, 7, 3, 7, 0, 9, 5, 5, 1, 6, 1, 6],<br \/>\n        &quot;265&quot;:[3, 6, 8, 9, 3, 4, 8, 8, 1, 4, 7, 4, 1, 9, 1, 0, 3, 2, 3, 2]<br \/>\n    };<br \/>\n    if (typeof(signed) != &#8216;boolean&#8217;) signed = false;<br \/>\n    function toBinary(hex) {<br \/>\n        hex = hex.toLowerCase();<br \/>\n        var binary = &quot;&quot;;<br \/>\n        for (var i = 0; i &lt; hex.length; i++) {<br \/>\n            binary += hexCode[hex[i]];<br \/>\n        }<br \/>\n        return binary;<br \/>\n    }<\/p>\n<p>    function to1nsComplement(binary) {<br \/>\n        var compliment = &quot;&quot;;<br \/>\n        for (var i = 0; i &lt; binary.length; i++) {<br \/>\n            compliment += (binary.charAt(i) == &quot;1&quot; ? &quot;0&quot; : &quot;1&quot;);<br \/>\n        }<br \/>\n        return compliment;<br \/>\n    }<\/p>\n<p>    function arrayAdd(a, b) {<br \/>\n        var carry = 0;<br \/>\n        var number = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];<br \/>\n        for (var i = 19; i &gt;= 0; i&#8211;) {<br \/>\n            number[i] = a[i] + b[i] + carry;<br \/>\n            if (number[i].toString().length &gt; 1) {<br \/>\n                carry = parseInt(number[i].toString().substring(0, number[i].toString().length &#8211; 1), 10);<br \/>\n                number[i] = parseInt(number[i].toString().substring(number[i].toString().length &#8211; 1), 10)<br \/>\n            } else {<br \/>\n                carry = 0;<br \/>\n            }<br \/>\n        }<br \/>\n        return number;<br \/>\n    }<\/p>\n<p>    function removeZeroPad(number) {<br \/>\n        var lock = false;<br \/>\n        var output = [];<br \/>\n        for (var i = 0; i &lt; number.length; i++) {<br \/>\n            if (lock) {<br \/>\n                output.push(number[i]);<br \/>\n            } else {<br \/>\n                if (number[i] != 0) {<br \/>\n                    lock = true;<br \/>\n                    output.push(number[i]);<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n        return output;<br \/>\n    }<\/p>\n<p>    function binaryToDec(binary) {<br \/>\n        var negative = false;<br \/>\n        if (signed &amp;&amp; (binary.charAt(0) == 1)) {<br \/>\n            negative = true;<br \/>\n        }<br \/>\n        if (signed) {<br \/>\n            binary = binary.substring(1);<br \/>\n            if (negative) {<br \/>\n                binary = to1nsComplement(binary);<br \/>\n            }<br \/>\n        }<br \/>\n        var pos = 0;<br \/>\n        var number = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];<br \/>\n        for (var i = binary.length &#8211; 1; i &gt;= 0; i&#8211;) {<br \/>\n            if (binary.charAt(i) == 1) {<br \/>\n                number = arrayAdd(number, preComputedLongMath[&quot;2&quot; + pos])<br \/>\n            }<br \/>\n            pos++;<br \/>\n        }<br \/>\n        if (negative) {<br \/>\n            number = removeZeroPad(arrayAdd(number, preComputedLongMath[&quot;20&quot;]));<br \/>\n            number.splice(0, 0, &quot;-&quot;);<br \/>\n        } else {<br \/>\n            number = removeZeroPad(number);<br \/>\n        }<br \/>\n        return number.join(&quot;&quot;);<br \/>\n    }<\/p>\n<p>    this.convert = function (hex) {<br \/>\n        var binary = toBinary(hex);<br \/>\n        return binaryToDec(binary);<br \/>\n    };<br \/>\n}<\/p>\n<p>[\/javascript]<\/p>\n<p>Hope it helps!!<br \/>\nRegards<br \/>\nKushal Likhi<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently in our Node.js application we had a task where we needed to convert the Windows hexadecimal Live Id (lid) to sighed 64 bit Integer such that it can be used in the API calls. But there was no direct way to convert that in JavaScript, but here i will discuss the workaround we took. [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":76},"categories":[1185],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9334"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=9334"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9334\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=9334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=9334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=9334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}