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.
迷茫2017-05-19 10:33:47
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