1

I am currently reading the ID number of the energy meter with Node JS and serialport library. The power meter ID has the following format xx xx xx xx xx xx. When I send the command and receive the data, I get the following DEC numbers: 0 0 24 1 104 115. Following the manufacturer's instructions, I have to convert this sequence to HEX. I have added it in an array and exported to the console as follows:

console.log(
        (arrID[0]).toString(16)+
          (arrID[1]).toString(16) +
          (arrID[2]).toString(16) +
          (arrID[3]).toString(16) +
          (arrID[4]).toString(16) +
          (arrID[5].toString(16)
      );

and it returned to me as follows 001816873. This is the wrong ID, The correct ID to show must be 000018016873. I know the reason is the conversion of numbers with the first character is 0. I look forward to advice from you.

Cosi Nguyen
  • 13
  • 1
  • 3
  • Possible duplicate of [How can I pad a value with leading zeros?](https://stackoverflow.com/questions/1267283/how-can-i-pad-a-value-with-leading-zeros) – Igal S. Mar 10 '19 at 12:07

1 Answers1

0

I used normal js, hope this helps you.

var arrID =[0, 0, 24, 1, 104, 115];
var arrID2 = ['','','','','',''];
for(var i=0;i<6;i++)
{
    arrID2[i]=(arrID[i]).toString(16);
    if(arrID2[i].length==1)arrID2[i]='0'+arrID2[i];
}

console.log(
  (arrID2[0])+
  (arrID2[1])+
  (arrID2[2])+
  (arrID2[3])+
  (arrID2[4])+
  (arrID2[5])
)

the output is

000018016873