Jump to content

Math in Hex() function


Recommended Posts

I ran into a strange issue today while updating one of my scripts. When I tried to do some simple division on a variable and then convert it to hex, I do not get the expected value returned.

An example:

$Size = 128*2
ConsoleWrite('128*2 = '&$Size&@CRLF)
ConsoleWrite('Hex() returns: '&hex($Size)&@CRLF)

$Size = 256
ConsoleWrite('256 = '&$Size&@CRLF)
ConsoleWrite('Hex() returns: '&hex($Size)&@CRLF)
$Size = 512/2
ConsoleWrite('512/2 = '&$Size&@CRLF)
ConsoleWrite('Hex() returns: '&hex($Size)&@CRLF)

Returns

128*2 = 256
Hex() returns: 00000100
256 = 256
Hex() returns: 00000100
512/2 = 256
Hex() returns: 4070000000000000

I am running version: 3.3.8.1

While I can work around this issue in the script, I would like to understand better what is going on.

Thank you,

Kerros

Kerros===============================================================How to learn scripting: Figure out enough to be dangerous, then ask for assistance.

Link to comment
Share on other sites

Thanks John that was the insight that I was needing. While I'm doing error checking earlier in the script to verify that the value is an int as I expect. I was not doing so right before sending to the hex function.

I tried doing the hex function on a double to verify your thought and I did get the expected result

ConsoleWrite('Hex on a double: '&hex(52.5)&@CRLF)
Output:
Hex on a double: 404A400000000000

Posting an example of the solution

$Size = Int(512/2)
ConsoleWrite('512/2 = '&$Size&@CRLF)
ConsoleWrite('Hex() returns: '&hex($Size)&@CRLF)

Output

Int(512/2) = 256
Hex() returns: 00000100

Kerros

Kerros===============================================================How to learn scripting: Figure out enough to be dangerous, then ask for assistance.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...