Bug/Caveat? ITOA() and negative SINTEGER CONSTANT
Joe Hebert
Junior Member
What?s a little sign bit between friends?
Consider the following code:
Output when button 1 is pushed:
Line 1 :: ITOA(snNeg275Const) = 65261 - 3:33:20
Line 2 :: ITOA(snNeg275Var) = -275 - 03:33:20
Output when button 2 is pushed:
Line 3 :: ITOA(NegConstPassedIn) = -275 - 03:33:23
ITOA() does not return negative numbers correctly when working on a SINTEGER CONSTANT as shown when button 1 is pushed.
However, if you pass the same SINTEGER CONSTANT into a function then ITOA() does return correctly as shown when button 2 is pushed.
Smells like a bug to me.
Reference:
Consider the following code:
DEFINE_DEVICE
dvTP = 10001:1:0
DEFINE_CONSTANT
SINTEGER snNeg275Const = -275
DEFINE_VARIABLE
SINTEGER snNeg275Var = -275
DEFINE_FUNCTION fnITOASignedConst (SINTEGER NegConstPassedIn) {
SEND_STRING 0, "'ITOA(NegConstPassedIn) = ',ITOA(NegConstPassedIn)"
}
DEFINE_EVENT
BUTTON_EVENT[dvTP,1] {
PUSH: {
SEND_STRING 0, "'ITOA(snNeg275Const) = ',ITOA(snNeg275Const)"
SEND_STRING 0, "'ITOA(snNeg275Var) = ',ITOA(snNeg275Var)"
}
}
BUTTON_EVENT[dvTP,2] {
PUSH: {
fnITOASignedConst(snNeg275Const)
}
}
Output when button 1 is pushed:
Line 1 :: ITOA(snNeg275Const) = 65261 - 3:33:20
Line 2 :: ITOA(snNeg275Var) = -275 - 03:33:20
Output when button 2 is pushed:
Line 3 :: ITOA(NegConstPassedIn) = -275 - 03:33:23
ITOA() does not return negative numbers correctly when working on a SINTEGER CONSTANT as shown when button 1 is pushed.
However, if you pass the same SINTEGER CONSTANT into a function then ITOA() does return correctly as shown when button 2 is pushed.
Smells like a bug to me.
Reference:
Netlinx Help File wrote:ITOA
Converts a 32-bit signed integer to a decimal ASCII string. The syntax:
CHAR[ ] ITOA (LONG Num)
Parameters:
Num - a 32-bit signed integer to convert to a decimal string.
Result:
A character string that contains the decimal representation of the specified integer.
Example:
STRING = ITOA(501) // STRING = '501'
Comments
-
Now I know what a bug smells like.
To quote one of my friends at AMX, "why would you want to do that!"Joe Hebert wrote:Output when button 2 is pushed:
Line 3 :: ITOA(NegConstPassedIn) = -275 - 03:33:23
However, if you pass the same SINTEGER CONSTANT into a function then ITOA() does return correctly as shown when button 2 is pushed.
Looks like the constant is converted to a variable as it is passed into the function.
Joe, send this link GM. -
Joe Hebert wrote:Output when button 1 is pushed:
Line 1 :: ITOA(snNeg275Const) = 65261 - 3:33:20
Line 2 :: ITOA(snNeg275Var) = -275 - 03:33:20
I think it's interesting, if obvious, that
65536 - 275 = 65261
so when displaying a negative constant it just subtracts the number from the maximum integer size (same thing that happens when you subtract a bigger non-signed integer from a smaller non-signed integer) -
I believe this is actually a bug in the way constants are treated when they are initialized, NOT an issue with ITOA().
I'll pass it along as necessary.
Best thing for now is to work the issue as described in previous posts... -
Note, ITOA() actually functions as SignedLong to ASCII.
This is considered a "feature", not a bug, as one will should get expected results with integers, signed or unsigned. Per the NetLinx Keywords Help: "converts a 32-bit signed integer to convert to a decimal string".
This is why you can get -275 in one case and 65261 in the other. Internally the sign bit is not at $8000 - as you would expect if the data were actually using only 16 bits.
Categories
- All Categories
- 2.5K AMX General Discussion
- 922 AMX Technical Discussion
- 514 AMX Hardware
- 502 AMX Control Products
- 3 AMX Video Distribution Products
- 9 AMX Networked AV (SVSI) Products
- AMX Workspace & Collaboration Products
- 3.4K AMX Software
- 151 AMX Resource Management Suite Software
- 386 AMX Design Tools
- 2.4K NetLinx Studio
- 135 Duet/Cafe Duet
- 248 NetLinx Modules & Duet Modules
- 57 AMX RPM Forum
- 228 MODPEDIA - The Public Repository of Modules for Everyone
- 943 AMX Specialty Forums
- 2.6K AMXForums Archive
- 2.6K AMXForums Archive Threads
- 1.5K AMX Hardware
- 432 AMX Applications and Solutions
- 249 Residential Forum
- 182 Tips and Tricks
- 146 AMX Website/Forums