[英]Javascript - parsing a hex value
I am trying to parse a hex value to decode a card 我正在尝试解析十六进制值以解码卡
The hex data I receive from the card is f8b2d501f8ff12e0056281ed55
我从卡收到的十六进制数据是f8b2d501f8ff12e0056281ed55
First I am converting this to an integer with parseInt() 首先,我使用parseInt()将其转换为整数
var parseData = parseInt('f8b2d501f8ff12e0056281ed55', 16);
The value recieved is 1.9703930145800871e+31
收到的值为1.9703930145800871e+31
When I try to decode this using the bitwise operator in Javascript 当我尝试使用Javascript中的按位运算符对此进行解码时
var cardNumber = ((parseData & 0xFFFFF) >> 1).toString();
I received a 0
value. 我收到一个0
值。
What am I doing wrong here, how can I parse the value of such large integer number? 我在这里做错什么,如何解析这么大的整数值?
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). 首先,请注意,代码中的& 0xFFFFF
操作等效于获取字符串的子字符串(最后5个字符)。
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. 第二种方法是使用任何大整数库,如果对大整数进行任何操作,我建议您使用它。
Since the number integer is so big you should use any bigNum library for js. 由于整数非常大,因此您应该为js使用任何bigNum库。
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. 我建议使用BigInteger ,因为您仅使用整数并且支持按位运算,但是您也可以检查此答案以获取更多选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.