Virtuino Forum
Javascript: dec to hex + slicing - Printable Version

+- Virtuino Forum (https://virtuino.com/forum)
+-- Forum: Virtuino (https://virtuino.com/forum/forumdisplay.php?fid=1)
+--- Forum: Virtuino IoT (https://virtuino.com/forum/forumdisplay.php?fid=2)
+--- Thread: Javascript: dec to hex + slicing (/showthread.php?tid=1267)



Javascript: dec to hex + slicing - Dkyndt - 04-19-2023

Hi, I have this javascript that should work.

However, the output does not result in HEX numbers.

var inputValue =  1224277079131226000.0 the
var hex = inputValue.toString(16).toUpperCase();
  var hexsliced = hex.slice(0,-4);
result = '{"tag":"outputTag", "value":"'+ hexsliced + '"}';


I expect the output to be "10FD81845845"
Currently it results "1224277079131226"

Can youplease help me out further?


RE: Javascript: dec to hex + slicing - iliaslamprou - 04-20-2023

Hi,
The input value is too big.
the "toString(16)" is not working for values with more than 16 digits.

you can use the "slice" before the conversion to hex. 
check the code below. The result is not correct but I hope to help you

Code:
var inputValue = +1224277079131226000.0;
var inputsliced = inputValue.toString().slice(0,-4);
var n= +inputsliced;
var hex=n.toString(16).toUpperCase();
res = '{"tag":"outputTag", "value":"'+ hex+ '"}';



RE: Javascript: dec to hex + slicing - Dkyndt - 04-20-2023

Thank you for your help.

I got something figured out.
When I first convert the long(64-bit) with your int -hex converter, I get the correct number, however I have some zeroes to much.

After this I can use this code to remove the 4 zero's.

Code:
//--- Copy values from Virtuino Variables to JavaScript variables
var inputValue = Virtuino.readValue("inputTag");        // read a Virtuino variable as String
 var hex = inputValue.toString(16);
//---  enter the JavaScript code like the example below
var value = hex.slice(0,-4);
result = '{"tag":"outputTag", "value":"'+ value+ '"}';    // return a json. Enter as "tag" the output tag you want to store the value