Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Javascript: dec to hex + slicing
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?

Attached Files Thumbnail(s)
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

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+ '"}';
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.

//--- 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

Forum Jump:

Users browsing this thread: 1 Guest(s)