1

I am trying to parse a hex value to decode a card

The hex data I receive from the card is f8b2d501f8ff12e0056281ed55

First I am converting this to an integer with parseInt()

    var parseData = parseInt('f8b2d501f8ff12e0056281ed55', 16);

The value recieved is 1.9703930145800871e+31

When I try to decode this using the bitwise operator in Javascript

    var cardNumber = ((parseData & 0xFFFFF) >> 1).toString();

I received a 0 value.

What am I doing wrong here, how can I parse the value of such large integer number?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Dhiraj
  • 2,687
  • 2
  • 10
  • 34
  • Your number is so big that it is transformed into a floating point number, and the rightmost digits are lost, i.e. set to 0. But if you are just interested in the rightmost 5 digits you can simply take a substring of your original hex number. – Renardo Mar 06 '18 at 16:23
  • What does the hex represent,.? If your saying this is a number, that would equal an 104 bit number so I would imagine it's not. Do you have the file format for what this card hex data represents. – Keith Mar 06 '18 at 16:35
  • 1
    Why you edited your question? – Dawid Loranc Mar 06 '18 at 17:22
  • The xx in `f9030xx22` are not valid hexadecimal characters. – Bergi Mar 06 '18 at 17:29
  • @Bergi, well I answered his question, and then he did some weird edits, as you can see in revisions history. I don't know why. – Dawid Loranc Mar 06 '18 at 17:38
  • @DawidLoranc I already tried to rollback - now flagging for mod attention. – Bergi Mar 06 '18 at 17:43
  • 1
    @Zuif, please do not significantly change your question, if you have a new problem then [ask a new question](https://stackoverflow.com/questions/ask) about it. Also your edited code doesn't make any sense, `parseInt('f903022', 1);` does return NaN not 1.9…e31. – Bergi Mar 06 '18 at 17:45

2 Answers2

2

There are two ways to do it:

First, notice that & 0xFFFFF operation in your code is just equivalent to getting a substring of a string (the last 5 characters).

So, you can just do a substring from the end of your number, and then do the rest:

var data = 'b543e1987aac6762f22ccaadd';
var substring = data.substr(-5);

var parseData = parseInt(substring, 16);
var cardNumber = ((parseData & 0xFFFFF) >> 1).toString();

document.write(cardNumber);

The second way is to use any big integer library, which I recommend to you if you do any operations on the large integers.

Dawid Loranc
  • 852
  • 2
  • 9
  • 22
  • @zuif, I changed. You should also edit you question and change `parseInt('f903022', 1);` to `parseInt('f903022', 16);`. – Dawid Loranc Mar 06 '18 at 23:41
0

Since the number integer is so big you should use any bigNum library for js.

I recommend BigInteger, since you are working only with integers and it supports bitwise operations, however you can also check this answer for more options.

Moyom
  • 516
  • 3
  • 9