JavaScript: Hex String To Int64 conversion (similar to Int64.parse)

30 / Dec / 2012 by Kushal Likhi 2 comments

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.

 

The problem with 64 bit Integer in JavaScript

Though javaScript numbers are 64 bit but they only use 53 bits to store the real value(mantissa), rest bits are used for exponent. Hence this makes holding of 64bit integer impossible in JavaScript.

 

The workaround Solution

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’s the only way.

 

Example: Converting hex windows live id to Integer Live Id (Signed 64bit Integer)

Following code will convert the received hex lid to intLid:
[javascript]
var lidHex = "b4fb0acfc47086c1"; //The Input Hex Number.
var lid64 = new HexStringToInt64StringConverter(true).convert(lidHex); //"true" is passed for Signed Conversion.
console.log(lid64); // output: -5405715040257931583
[/javascript]
Note: Implementation of HexStringToInt64StringConverter is discussed below.

 

Example: Converting hex number to Integer (Unsigned 64bit Integer)

Following code will convert the received hex lid to intLid:
[javascript]
var intHex = "b4fb0acfc47086c1"; //The Input Hex Number.
var int64 = new HexStringToInt64StringConverter(false).convert(intHex); //"false" is passed for UnSigned Conversion.
console.log(int64); // output: 13041029033451620033
[/javascript]
Note: Implementation of HexStringToInt64StringConverter is discussed below.

 

Implementation: HexStringToInt64StringConverter

This converter is implemented with following points in mind:

  1. 2’s Complement Signed Representation
  2. Number represented in Array for arithmetic manipulations.
  3. All Powers of 2 which are impossible to calculate in JavaScript are pre-calculated and hard coded for faster conversions.
  4. Output number to be represented as a string.
  5. Both signed and unsigned conversions.

 

[javascript]
function HexStringToInt64StringConverter(signed) {
var hexCode = {
‘0’:"0000",
‘1’:"0001",
‘2’:"0010",
‘3’:"0011",
‘4’:"0100",
‘5’:"0101",
‘6’:"0110",
‘7’:"0111",
‘8’:"1000",
‘9’:"1001",
‘a’:"1010",
‘b’:"1011",
‘c’:"1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, 3, 7, 2, 0, 3, 6, 8, 5, 4, 7, 7, 5, 8, 0, 8],
"264":[1, 8, 4, 4, 6, 7, 4, 4, 0, 7, 3, 7, 0, 9, 5, 5, 1, 6, 1, 6],
"265":[3, 6, 8, 9, 3, 4, 8, 8, 1, 4, 7, 4, 1, 9, 1, 0, 3, 2, 3, 2]
};
if (typeof(signed) != ‘boolean’) signed = false;
function toBinary(hex) {
hex = hex.toLowerCase();
var binary = "";
for (var i = 0; i < hex.length; i++) {
binary += hexCode[hex[i]];
}
return binary;
}

function to1nsComplement(binary) {
var compliment = "";
for (var i = 0; i < binary.length; i++) {
compliment += (binary.charAt(i) == "1" ? "0" : "1");
}
return compliment;
}

function arrayAdd(a, b) {
var carry = 0;
var number = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
for (var i = 19; i >= 0; i–) {
number[i] = a[i] + b[i] + carry;
if (number[i].toString().length > 1) {
carry = parseInt(number[i].toString().substring(0, number[i].toString().length – 1), 10);
number[i] = parseInt(number[i].toString().substring(number[i].toString().length – 1), 10)
} else {
carry = 0;
}
}
return number;
}

function removeZeroPad(number) {
var lock = false;
var output = [];
for (var i = 0; i < number.length; i++) {
if (lock) {
output.push(number[i]);
} else {
if (number[i] != 0) {
lock = true;
output.push(number[i]);
}
}
}
return output;
}

function binaryToDec(binary) {
var negative = false;
if (signed && (binary.charAt(0) == 1)) {
negative = true;
}
if (signed) {
binary = binary.substring(1);
if (negative) {
binary = to1nsComplement(binary);
}
}
var pos = 0;
var number = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
for (var i = binary.length – 1; i >= 0; i–) {
if (binary.charAt(i) == 1) {
number = arrayAdd(number, preComputedLongMath["2" + pos])
}
pos++;
}
if (negative) {
number = removeZeroPad(arrayAdd(number, preComputedLongMath["20"]));
number.splice(0, 0, "-");
} else {
number = removeZeroPad(number);
}
return number.join("");
}

this.convert = function (hex) {
var binary = toBinary(hex);
return binaryToDec(binary);
};
}

[/javascript]

Hope it helps!!
Regards
Kushal Likhi

FOUND THIS USEFUL? SHARE IT

comments (2)

Leave a Reply to Roong Cancel reply

Your email address will not be published. Required fields are marked *