Converting Decimal to Hex in Storyline

I'm trying to convert a user input number from Decimal to Hex using an Execute JavaScript command on the click of a button. I've checked the code in https://www.w3schools.com/jsref/jsref_tostring_number.asp and all seems to be OK, but when I execute the command, it returns the decimal value. Can anyone give me a steer on where I might be going wrong?

Variables set within the system are:
NumericEntry        Number         (number input by user)
HexOutput              Text               (converted value)

Code is:

//Convert Decimal to Hex
var player = GetPlayer();
var hex = player.GetVar("NumericEntry");
player.SetVar("HexOutput", hex.toString(16).toUpperCase());


Thanks

Chris

4 Replies
Najam  Fazal

I couldn't find any reference material except this. What I find by experimenting is that Numeric Entry creates a string type value. You can check it by using a text variable or console.log and run typeof player.GetVar("NumericEntry"). It will return "string" as type. Please note that Numeric Entry Field and the variable NumericEntry are two different things; one is user input element while other is a number type variable in SL.

I also tested by making an add button that, when clicked, adds 5 to the NumericEntry variable using the adjust variable action. As expected, it adds 5 each time to the NumericEntry. So how come this time it is taking NumericEntry as Integer and not treating it as string?

What's happening under the hood is that Numeric Entry field uses Text Input Field and when we use adjust variable action with Add/Subtract/Multiple/Divide operators, SL is parsing the string (from Numeric Entry field) to int/dec and then performing the operator on the values. However, in the case where we directly execute JS on NumericEntry, it is coming straight from Text Input Field (as string) and we have to parse it on our own i-e. convert string value from Numeric Entry field to Integer (base 10).

For reference see: parseInt() and typeof. This is my analysis and I can be wrong. I do not find any detailed explanation on this site. If you come across any, please share it here for future visitors.

Chris Clift

Well explained, thanks, that explains why, when I did something similar, but this time adding 1 to decrement/increment the decimal value to create a hex equivalent it worked, but when trying to do a direct convert it didn't. Thanks for all you're advice on this, Najam, now all I have to do is work out why localStorage is not working in Internet Explorer:(