Home AMX User Forum NetLinx Studio
Options

Weird Quirk with ITOA

Was doing some troubleshooting on a CRC calculation and noticed that when I used ITOA to show some data it gave me some odd results
button_event[10128:1:0,1]
{
   push:
   {
      stack_var integer crc
      crc = $ffff
      send_string 0,"'Test 1:',itoa(crc << 8)"
      send_string 0,"'Test 2:',itoa(crc + 1)"
   }
}

Returned:
Test 1:16776960
Test 2:0

Which is odd because I expected Test 1 to be $FF00 not $FFFF00. In digging I found that ITOA's parameter is actually a Long which kinda explains the first result, but then it's not consistent with the second result?

Comments

  • Options
    Joe HebertJoe Hebert Posts: 2,159
    If you change:
    send_string 0,"'Test 1:',itoa(crc << 8)"

    To this equivalent:
    send_string 0,"'Test 1:',itoa(crc * 256)"

    Then you get the expected result of 65280 ($FF00)

    Weird indeed.
    alexanbo wrote: »
    Was doing some troubleshooting on a CRC calculation and noticed that when I used ITOA to show some data it gave me some odd results
    button_event[10128:1:0,1]
    {
       push:
       {
          stack_var integer crc
          crc = $ffff
          send_string 0,"'Test 1:',itoa(crc << 8)"
          send_string 0,"'Test 2:',itoa(crc + 1)"
       }
    }
    

    Returned:
    Test 1:16776960
    Test 2:0

    Which is odd because I expected Test 1 to be $FF00 not $FFFF00. In digging I found that ITOA's parameter is actually a Long which kinda explains the first result, but then it's not consistent with the second result?
  • Options
    mpullinmpullin Posts: 949
    It might be an odd behavior of the bitwise shove (<<) as opposed to itoa. What happens if you remove itoa and just print the numbers?
  • Options
    alexanboalexanbo Posts: 282
    I think it is in itoa, because the calcuation was actually being done correctly, just when I tried to print out intemediary values with itoa that it got funky, sort of a Schr?dinger's Cat sort of thing.

    For example if you added a line before the print statement
    crc = $ffff << 8

    and changed the print statement to:
    send_string 0,"'Test1:',itoa(crc)"

    it would print out the $ff00 decimal equivalent
  • Options
    jweatherjweather Posts: 320
    alexanbo wrote: »
    I think it is in itoa, because the calcuation was actually being done correctly, just when I tried to print out intemediary values with itoa that it got funky, sort of a Schr?dinger's Cat sort of thing.

    For example if you added a line before the print statement
    crc = $ffff << 8

    and changed the print statement to:
    send_string 0,"'Test1:',itoa(crc)"

    it would print out the $ff00 decimal equivalent

    Yes, because crc is an integer (16 bits).

    If you said
    STACK_VAR LONG_INTEGER test
    test = $ffff << 8 // or test = $ffff * 256
    

    then the result is $ffff00, because test is a 32-bit integer.

    It's nothing to do with itoa aside from the fact that its argument is a LONG_INTEGER. The $ffff+1 test may be running into some weird signed/unsigned arithmetic as well.
Sign In or Register to comment.