Home AMX User Forum NetLinx Studio

String lenght

I have a problem receiving different lenght of EIB EIS5 value (temperature).

Sometimes it is 7.00 (4 caracters) and sometime 10.00 (5 caracters)

How to make it always 5 characterm in fact how to add ZERO (0) in front in case that i have less than 5 caracters ????

Comments

  • viningvining Posts: 4,368
    You could do something like this:
    DEFINE_VARIABLE
    
    VOLATILE CHAR cTemperature[5] ;
    
    DATA_EVENT [dvDevice]     
         
         {
         STRING:
    	  {
    	  STACK_VAR CHAR cTempSTR[6] ;
    	    
    	  cTempSTR = "'0',DATA.TEXT" ;
    	  cTemperature = right_string(cTempSTR,5) ;
    	  }
         }
    
  • VladaPUBVladaPUB Posts: 139
    I still have a problem, string that I am receiving is comming from EIB and look like
    CHAR sEIS521[5]
    

    and i need to send it to another device like here
    DATA_EVENT[vdvEIB]			
    {
    	STRING:
    	{
    		STACK_VAR CHAR sCmd[50] 
    		STACK_VAR INTEGER nTableNum 
    		sCmd = REMOVE_STRING(DATA.TEXT,"'='",1)
    		SWITCH(sCmd)
    		{
    			CASE 'EIS5=':
    			{
    				nTableNum = ATOI(DATA.TEXT)
    				REMOVE_STRING(DATA.TEXT,"':'",1)
    				SWITCH(nTableNum) // what table id
    				{
    					CASE 21:
    					{
    					sEIS521= "DATA.TEXT" 
    					SEND_COMMAND dvPanel,"'@TXT',61,sEIS521,'?'"
    					SEND_STRING dvPronto,"'TEMPZ',sEIS521,'TEMPM',sEIS516"
    					
    					}
    				}
    }}}
    
    


    Can you show me that on this example how should look ?
  • viningvining Posts: 4,368
    Sorry, but I don't see how the original post fits in with your last post's code. Me thinks there's something missing. What does the actual returned string look like?
  • yuriyuri Posts: 861
    vining wrote:
    You could do something like this:
    DEFINE_VARIABLE
    
    VOLATILE CHAR cTemperature[5] ;
    
    DATA_EVENT [dvDevice]     
         
         {
         STRING:
    	  {
    	  STACK_VAR CHAR cTempSTR[6] ;
    	    
    	  cTempSTR = "'0',DATA.TEXT" ;
    	  cTemperature = right_string(cTempSTR,5) ;
    	  }
         }
    

    would you really do it like that?
    Wouldn't it be ALOT nicer to use FORMAT()?

    like this:
    FORMAT(%05.f,cTemperature)
    

    this *should* format the string so that it is always 5 characters long, and has a precision of 2 decimals right of the decimal point...

    disclaimer:
    the format bit it psuedo code, but you get my point :p
  • viningvining Posts: 4,368
    Yuri wrote:
    Wouldn't it be ALOT nicer to use FORMAT()?
    I'm sure that will work if you say so but to be honest I've never got around to using that command yet.

    I think this would work as well, just changing cTemperature[5] as it would drop the added zero if 5 characters are returned and keep if if only 4 characters are returned.
    cTemperature = "'0',DATA.TEXT" ;
    

    I obviously don't always go wtih the shortest, cleanest method and generally write so I can easily tell what's going on in th furture.
  • Joe HebertJoe Hebert Posts: 2,159
    FORMAT
    yuri wrote:
    would you really do it like that?
    Wouldn't it be ALOT nicer to use FORMAT()?

    like this:
    FORMAT(%05.f,cTemperature)
    

    this *should* format the string so that it is always 5 characters long, and has a precision of 2 decimals right of the decimal point...

    disclaimer:
    the format bit it psuedo code, but you get my point :p
    I?m a big fan of FORMAT also and prefer it over string manipulation but in this case the FORMAT command won?t work as intended (even disregarding the ?pseudo? code that?s not formatted correctly :) ) since cTemperature is already ASCII.

    If you want to use the FORMAT command with vining?s CHAR cTemperature[5] then one way to do it is like this:

    SEND_STRING 0, "FORMAT('%05.2f',ATOF(cTemperature))"

    If you were starting with FLOAT fTemperature then you would do it like this:
    SEND_STRING 0, "FORMAT('%05.2f',fTemp)"

    Both of the above examples will return a 2 digit precision after the decimal point, force a 5 character minimum width, and pad with leading 0s if need be.
    vining wrote:
    I think this would work as well, just changing cTemperature[5] as it would drop the added zero if 5 characters are returned and keep if if only 4 characters are returned.
    cTemperature = "'0',DATA.TEXT" ;
    
    If 5 characters are returned in DATA.TEST then the ?0? that you are appending won?t be dropped, the last character will be dropped instead. So if DATA.TEXT = ?12.34? then the result of ??0?,DATA.TEXT? will be ?012.3?

    To each their own as far as programming style, whatever works best for the individual. However, I think the FORMAT command is extremely powerful and worth learning.
  • viningvining Posts: 4,368
    Joe Hebert wrote:
    If 5 characters are returned in DATA.TEST then the ?0? that you are appending won?t be dropped, the last character will be dropped instead. So if DATA.TEXT = ?12.34? then the result of ??0?,DATA.TEXT? will be ?012.3?

    and then:

    I think the FORMAT command is extremely powerful and worth learning.
    First I need to learn the basics like which bit gets drop when adding to a string! :o I swore it was the other way around and shifted from right to left. Kinda like when watching a buffer in debug. Fills from the right and pushes the originl contents out on the left.

    Every time I'm start feeling smart I realize how stupid I still am. Damm!
  • Joe HebertJoe Hebert Posts: 2,159
    First of all I should have written prepending not appending in my previous post since that?s what you were doing with the ?0?.
    vining wrote:
    I swore it was the other way around and shifted from right to left. Kinda like when watching a buffer in debug. Fills from the right and pushes the originl contents out on the left.
    There is no shifting going on assuming you?re talking about watching a buffer something like this:
    cBuffer = ?cBuffer,DATA.TEXT?

    DATA.TEXT keeps getting appended to the end of cBuffer and if cBuffer gets full then any remaining bytes of DATA.TEXT gets lost. (If Netlinx didn?t keep us safe in the sandbox then the remaining bytes would get written to the next chunk of memory which could be disastrous as you could possibly write over the program itself.)

    Maybe you?re thinking about GET_BUFFER_CHAR? In that instance position 1 of the buffer gets extracted and all the remaining bytes shift to the left by one.
    vinig wrote:
    Every time I'm start feeling smart I realize how stupid I still am.
    Been there done that more times than I?d care to admit. But that?s what makes it so much fun. There is always room to learn and grow.
  • yuriyuri Posts: 861
    Joe Hebert wrote: »
    I?m a big fan of FORMAT also and prefer it over string manipulation but in this case the FORMAT command won?t work as intended (even disregarding the ?pseudo? code that?s not formatted correctly :) ) since cTemperature is already ASCII.

    If you want to use the FORMAT command with vining?s CHAR cTemperature[5] then one way to do it is like this:

    SEND_STRING 0, "FORMAT('%05.2f',ATOF(cTemperature))"

    If you were starting with FLOAT fTemperature then you would do it like this:
    SEND_STRING 0, "FORMAT('%05.2f',fTemp)"

    Both of the above examples will return a 2 digit precision after the decimal point, force a 5 character minimum width, and pad with leading 0s if need be.


    If 5 characters are returned in DATA.TEST then the ?0? that you are appending won?t be dropped, the last character will be dropped instead. So if DATA.TEXT = ?12.34? then the result of ??0?,DATA.TEXT? will be ?012.3?

    To each their own as far as programming style, whatever works best for the individual. However, I think the FORMAT command is extremely powerful and worth learning.

    you're right. It was late and i didn't have my trusty Netlinx Studio at hand for help :P
  • VladaPUBVladaPUB Posts: 139
    I am trying it like this
    sEIS521= "'0',DATA.TEXT" 
    SEND_STRING vdvPronto,"'TEMPZ',FORMAT('%05.2f',sEIS521),'TEMPM',FORMAT('%05.2f',sEIS516)"
    

    but it sending
     TEMPZ08.00TEMPM022.0
    

    How to reformat it to remove first 0 ?
  • VladaPUBVladaPUB Posts: 139
    I have solve problem like this
    sEIS521= FORMAT('%05.2f',ATOF(DATA.TEXT))
    
Sign In or Register to comment.