Home AMX User Forum AMX General Discussion

Integer parsing from a char variable?

I have a char string that I want to parse the numeric portion out of. I know the numeric portion starts at character 6 everytime. (I am checking a variable that gets the TP's keypad entry dumped into it, so I know it will always start with KEYP-). The purpose of this is to take the keypad's number entry, place that number into an integer variable, and then execute a timeline. Is this possible with my train of thought, or is there a better way to do this?

Comments

  • use ATOI which converts ASCII to INTEGER.
    Check these forums and the tech notes for examples

    Very simply
    
    IF ( FIND_STRING ( DATA.TEXT, "KEYP-", 1) )
    {
    REMOVE_STRING (DATA.TEXT, "KEYP-", 1)
    nSOMEVARIABLE = ATOI (LEFT_STRING ( DATA.TEXT, 1) )
    }
    
    


    TN# 616 is a great example on how to parse buffers. You can use many methods within that basic structure to get the characters and numbers you want. Also try MID_STRING: you can start counting halfway through the string if you want and extract only the number of characters you want.
  • OMG I got it working!

    For anyone interested, here is the string parsing and string to decimal conversion process that worked for me:
    DATA_EVENT[dvJVCLT37X898Tp] //keypad entries
    {
    STRING:
    	{
    		sKeypad = DATA.TEXT
    		
    			
    			IF (FIND_STRING(sKeypad,'KEYP-',1))
    				{
    					REMOVE_STRING (sKeypad, 'KEYP-',1)
    					
    					nsleep = ATOI(sKeypad)
    					nsleep = nsleep*60
    					
    					SEND_COMMAND dvJVCLT37X898Tp, "'^TXT-10,0,',nSleep"
    					TimeArray[1] = 1000
    					TIMELINE_CREATE(MBedSleep, TimeArray, 1,TIMELINE_ABSOLUTE,TIMELINE_REPEAT)
    				}
            }
    }
    

    Now I can enter minutes into the onboard keypad, and it starts to decrement showing the correct format, based on the following timeline event and formatting:
    TIMELINE_EVENT[MBedSleep]  //sleep timer, captures all events
    {
    	SWITCH(TIMELINE.SEQUENCE)
    	{
    	CASE 1:
    		{
    			nminutes = nSleep/60
    			nseconds = nSleep mod 60
    			nSleep--
    			If (nSleep>0)
    			{
    				SEND_COMMAND dvJVCLT37X898Tp,"'^TXT-10,0,',ITOA(nminutes),':',right_string("'00',itoa(nseconds)",2)"
    			}
    		
    			ELSE
    			{
    				SEND_COMMAND dvJVCLT37X898Tp,"'^TXT-10,0,ROOMOFF'"
    				SEND_COMMAND vdvJVCLT37X898, "'PASSTHRU-!',$82,$80,'PW0',$0A"
    				TIMELINE_KILL(MBedSleep)
    			}
    		}
    	}
    }
    

    I feel so accomplished. I couldn't have done this without all of you. Thank you! It's not pretty, but hey, that's what the TPDesign course is for, right?
Sign In or Register to comment.