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


Forum Jump:


Users browsing this thread: 2 Guest(s)