Home AMX User Forum NetLinx Studio

Negative Integers

DATA_EVENT[dvAUDIO_SWITCH]
{
    STRING:
    {
	STACK_VAR CHAR sMYBUF[50]
	STACK_VAR INTEGER nSTRING_LENGTH
	STACK_VAR INTEGER nAUDIO_SWITCH_OUTPUT
	WHILE(FIND_STRING(sAUDIO_RXBUF,'SL2',1)) // WHILE THE MESSAGE IS A STATUS UPDATE
	{
	    REMOVE_STRING(sAUDIO_RXBUF,'SL2',1)
	    sMYBUF = REMOVE_STRING(sAUDIO_RXBUF,')',1)
	    IF(LEFT_STRING(sMYBUF,1) == 'O')
	    {
		GET_BUFFER_CHAR(sMYBUF)
		nAUDIO_SWITCH_OUTPUT = ATOI(LEFT_STRING(sMYBUF,1))
		REMOVE_STRING(sMYBUF,' ',1)
		nSTRING_LENGTH = LENGTH_STRING(sMYBUF)
		sMYBUF = LEFT_STRING(sMYBUF,(nSTRING_LENGTH - 2))
		sAUDIO_SWITCH_VOLUME = sMYBUF
		nSTRING_LENGTH = LENGTH_STRING(sMYBUF)
		IF(FIND_STRING(sAUDIO_SWITCH_VOLUME,'M',1))
		{
		    nAUDIO_SWITCH_VOLUME[nAUDIO_SWITCH_OUTPUT] = -700
		}
		ELSE IF(FIND_STRING(sAUDIO_SWITCH_VOLUME,'-',1))
		{
		    nAUDIO_SWITCH_VOLUME[nAUDIO_SWITCH_OUTPUT] = (ATOI(RIGHT_STRING(sAUDIO_SWITCH_VOLUME,(nSTRING_LENGTH -1))) * (-1))
		}
		ELSE
		{
		    nAUDIO_SWITCH_VOLUME[nAUDIO_SWITCH_OUTPUT] = ATOI(sAUDIO_SWITCH_VOLUME)
		}
		REMOVE_STRING(sMYBUF,LEFT_STRING(sMYBUF,nSTRING_LENGTH),1)
		fnAUDIO_SWITCH_VOLUME_UPDATE()
	    }
	}
    }
}

Why if sAUDIO_SWITCH_VOLUME is '-150' does my integer nAUDIO_SWITCH_VOLUME[] store as 65386 instead of -150? I know that 65386 - 65536 = -150 but I don't get why it is storing info that way. nAUDIO_SWITCH_VOLUME is defined as an sinteger.

Comments

  • By the way... my bargraph with a range of -700 to 100 updates just fine with this code.
  • AMXJeffAMXJeff Posts: 450
    What your seeing in the value is the way integers store negative numbers, they store negative number by using "two's" complement, hopefully this will explain it...

    http://en.wikipedia.org/wiki/Two's_complement
  • You have your variable declared as an Integer, which in Netlinx is positive only. You're seeing the "wrap-around"... as your value goes negative the variable is wrapping-around to 65535. If you need negative numbers you should declare your variable as a signed integer.

    --D
  • That makes sense. What I still don't get is sometimes when I store a negative value to an sinteger and I look at the value in debug sometimes it will show the negative value and others it shows the "65386-type" number. How can I make my value just display as -150?
  • SINTEGER nAUDIO_SWITCH_VOLUME[18]
    

    This is my variable declaration and I'm still getting issues.
  • remeolb wrote: »
    That makes sense. What I still don't get is sometimes when I store a negative value to an sinteger and I look at the value in debug sometimes it will show the negative value and others it shows the "65386-type" number. How can I make my value just display as -150?

    I actually don't believe that I've ever seen the debugger show a negative number for a sinteger. If there's a way to do that, I'll be very happy.
  • Here's a screen shot from the debugger for a program where my variable shows as negative. And here is the code...
    DATA_EVENT[dvREF_70]
    {
    	ONLINE:
    	{
    		SEND_COMMAND dvREF_70,"'SET BAUD 9600,N,8,1'"
    	}
    	STRING:
    	{
    	    SEND_COMMAND dvTP_REF_70,"'^TXT-1,0,',DATA.TEXT"
    	    IF (MID_STRING(DATA.TEXT,5,1) == 'R')		// IF STRING IS A STATUS MESSAGE
    	    {
    		ON[nRECEIVE_STRING]
    		nVOL_1 = TYPE_CAST(HEXTOI(MID_STRING(DATA.TEXT,17,1)))
    		nVOL_2 = TYPE_CAST(HEXTOI(MID_STRING(DATA.TEXT,18,1)))
    		nBK_VOLUME = ((16*nVOL_1) + nVOL_2)-96		// SET UP POLLING SO THAT VOLUME NUMBER IS ALWAYS ACCURATE
    		IF (nBK_VOLUME <= 0)				// NEGATIVE OR ZERO VOLUME
    		{
    		    sBK_VOLUME = ITOA(nBK_VOLUME)
    		}
    		ELSE						// POSITIVE VOLUME
    		{
    		    sBK_VOLUME = "'+',ITOA(nBK_VOLUME)"
    		}
    		SEND_COMMAND dvTP_OFFICE_REF_70,"'^TXT-2,0,',sBK_VOLUME"
    		SEND_COMMAND dvTP_REF_70,"'^TXT-2,0,',sBK_VOLUME"
    	    }
    	    ELSE
    	    {
    		OFF[nRECEIVE_STRING]
    	    }
    	}
    }
    
  • Can you have SINTEGERs in an array?
Sign In or Register to comment.