• # Script to convert from Hex to Dec

Hello, the snmp device send the data in hexadecimal format for example: Battery voltage = 12:7D, I want to solve it with a Metadata Source, but I dont have information how I can create a script.
Please could You Help me with this script and could you share information about how to create scripts???.

Jorge G

• Hi Jorge

you can use parseInt for this:
`return parseInt(p.value, 16)`

• return parseInt(p.value, 16)

Thanks so much Craig, but it didnt work. please let me to explain You:
the hex data is arriving as 13:32 (it is Hex 1332, so must be in Dec Value=4194)
With your Script I got the Dec value from just the first number (Hex13 =Dec19)
this device send a string like 13:30:27:14:01:33:02:6c:02:76.
then I need to get
13:32:4194
27:14=10000
01:33=307
02:6c=620
02:76=630

After i get the values, then we will need to scale it:
41950,01=41,95
(10000
0.1)-1000=0
3070.1=30,7
(620
0.1)-40=22
(630*0.1)-40=23

I understand how to define a script to multiply, but some data need to be multiply and the final result will be minus 40, for example:
Data 02:76 = Hex630= ((630*0,1)-40)=23

As you can see im in a problem, please could you support me

Jorge G

• 13:32

@Jorge-Gonzalez, documentation to solve your problems.

Firstly see what parseInt does - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

There you can see that the hexdecimal value has to be in correct format be it with 0x or without. Your string splits 2 bytes in the hexdecimal format by using a colon. You need to remove it. Simply do it by utilizing .replace method on a string before putting it into parseInt. Or if you have a string which contains multiple values utilize .split method on a string and then put the bytes together before putting the value into parseInt.

Replace method explanation
https://www.w3schools.com/jsref/jsref_replace.asp

Split method explanation
https://www.w3schools.com/jsref/jsref_split.asp

Following function is following the detailed explanation you provided in the post.

``````function stringsplitter (string) {

var result = {};

// This contains the bytes of data
result.arrayofbytes = string.split(':');

// Get byte count of data inside the string
result.bytecount = result.arrayofbytes.length;

// Start at the beginning
var variablecounter = 0;

result.hexdata = [];
result.decimaldata = [];

// Loop over the array, but taking into account that we have 2 byte data, use try/catch so that function will return if something should fail
try{
for (var x = 0;x < result.bytecount / 2;x++){

result.hexdata[x] = result.arrayofbytes[variablecounter] + result.arrayofbytes[variablecounter+1];
result.decimaldata[x] = parseInt(result.hexdata[x],16);
variablecounter += 2;

}
}catch(err){
return err
}

// return object with all our data which includes hexdecimal data, decimaldata and byte count inside the string
return result;

}

// Example usage

var devicestring = '13:30:27:14:01:33:02:6c:02:76';

var data = stringsplitter(devicestring);

//get array in hexdecimal and decimal format
var hexdata = data.hexdata;
var decimaldata = data.decimaldata;

// first value in hexdecimal and decimal format
var firstvaluehex = hexdata[0];
var firstvaluedecimal = decimaldata[0];

// Second value in hexdecimal and decimal format
var secondvaluehex = hexdata[1];
var secondvaluedecimal = decimaldata[1];

// Following that your post had used an example with the last value

var lastvalue = decimaldata[4];

// Should be 23
var lastvaluecorrected = lastvalue * 0.1 - 40;

``````

Thomas

Posts 4Views 148