javascript - How does js handle data with low-end first and high-end last?
滿天的星座
滿天的星座 2017-05-19 10:31:47
0
1
510

Like the title

During the interaction with the hardware device, a hexadecimal number is received from the hardware, with the low digit in front and the high digit in the back. How to elegantly parse it into the correct decimal data?

//举例 16进制的一个数据
var num=0x12345678
//因为低位在前,高位在后,正确解析后是
num=0x78563412
//转换成10进制是
num=2018915346

Current processing method:
Cut num according to length, then change the order before and after through a for loop, and finally convert parseInt to decimal, which achieves the goal. I feel that this method is definitely unreasonable. I am looking for a more standard approach.

滿天的星座
滿天的星座

reply all(1)
迷茫

There is a problem with the above one. According to the original poster, the correct way to write it is as follows:

function translate(num){
  return parseInt('0x' + num.match(/\d{2}/g).reverse().join(''), 16);
}
translate('0x12345678'); //2018915346
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!