Javascript hex to number one

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

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’:»1100″,
‘d’:»1101″,
‘e’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, 4, 2, 7, 3, 8, 7, 9, 0, 4],
«263»:[0, 9, 2, 2, 3, 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;
>
Читайте также:  What is parameters in python

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);
>;
>

Hope it helps!!
Regards
Kushal Likhi

Источник

Convert Hex to Decimal in JavaScript

Convert Hex to Decimal in JavaScript

In this post, we’ll learn how to convert hexadecimal numbers to decimal in JavaScript.

To do this, we will write a function that takes a hexadecimal string as an argument and returns its decimal representation.

For example, we want the following input to return the following output:

parseInt()

The best way to convert a hexadecimal number to decimal is to make use of the parseInt() function.

Читайте также:  Все виды меню на css

The parseInt() function takes a string as an argument and returns an integer.

The first argument of it is the string to be converted, and the second argument is the radix (base) of the number.

Since we are converting to a hexadecimal number, the radix is 16, since there are 16 hexadecimal digits.

With this, we can create a function called hexToDecimal that takes a hexadecimal string and returns its decimal equivalent.

 parseInt(hex, 16); const dec1 = hexToDecimal("2"); const dec2 = hexToDecimal("35"); const dec3 = hexToDecimal("1f4"); const dec4 = hexToDecimal("7b2"); const dec5 = hexToDecimal("123abc"); console.log(dec1); // 2 console.log(dec2); // 53 console.log(dec3); // 500 console.log(dec4); // 1970 console.log(dec5); // 1194684 

Our function is working as intended, converting our hexadecimal numbers to decimal!

Conclusion

We’ve seen how you can use parseInt() in JavaScript to convert a hexadecimal number to decimal.

Hopefully, this post has been useful to you and you can use it to convert hexadecimal numbers to decimal in your own projects.

If you want to learn about web development, founding a start-up, bootstrapping a SaaS, and more, follow me on Twitter! You can also join the conversation over at our official Discord!

Give feedback on this page , tweet at us, or join our Discord !

Источник

Converting (binary,hex,octal) to decimal numbers in JavaScript

In this tutorial, we are going to learn about how to convert binary, hexadecimal and octal to decimal numbers in JavaScript.

In JavaScript, we have the parseInt() method by using that we can make conversions.

The parseInt( ) method takes the two arguments, the first argument is string and the second argument is base by using that it returns the decimal number of a specified base.

Читайте также:  Syntax error python expected

Let’s see the conversions with the help of examples.

Converting binary to decimal

To convert a binary to decimal we need to pass base 2 as a second argument to the parseInt method.

 const binaryNumber = "1100"; console.log(parseInt(binaryNumber,2)); // base 2 //output--> 12

Converting hexadecimal to decimal

To convert a hexadecimal to decimal we need to pass base 16 as a second argument to the parseInt method.

 const hex = "c"; console.log(parseInt(hex,16)); //hex base-16 //output --> 12

Converting octal to decimal

To convert a octal to decimal we need to pass base 8 as a second argument to the parseInt method.

const octal = "14"; console.log(parseInt(octal,8)); //octal base-8 //output --> 12

Источник

Оцените статью