Home AMX User Forum NetLinx Studio

Getting Integer Value from a String


I am trying to get an integer value from a string returned from a projector. The second of three bytes in the string is a 1 when the unit is on and a 0 when it's off. A copy/paste of my code follows. Rather than simplifying it, I'm posting it as it is so that someone may be able to spot my error. nPROJ_POWER_STATUS is what I am trying to set to 1 or 0 according to the string returned. It is an INTEGER array as there is more than one projector. dPROJECTORS is a DEV array of the projectors. cPROJECTOR_MESSAGE is the buffer filled via DATA.TEXT during a STRING: DATA_EVENT. The string is definitely coming in properly as I have viewed it in debug. Without the ATOI, I get a warning on type conversion.


Thank you!


  • Options
    Getting Integer Value from a String

    Hi -- I am a little unclear on your question. Is the code sample you provided not working (nPROJ_POWER_STATUS not being set to 0 or 1) or is it that the ATOI() function is required in order to avoid the warning? If the latter, the ATOI() is needed since otherwise you would be doing a string to integer conversion since MID_STRING() returns CHAR [].

    Otherwise, with the ATOI(), I don't see anything wrong with the code that would not yield a 0 or 1 for nPROJ_POWER_STATUS. Although your code looks fine, I would probably have coded it differently in my application to avoid the need for the ATOI() and MID_STRING() provided the only values I was looking for were '0' or '1'.
    nPROJECTOR = GET_LAST(dPROJECTORS)     // compute once for efficiency
    (* Set power status to 1 if '1' is in string - 0 otherwise *)

    Of course, my code depends on the string being examined always containing the power status indicator in position 2 (as does yours). If this will not always be the case, then parsing the string to determine the location of the power status indicator might be needed as well. Since you are only interested in examining a single character in the string, MID_STRING() seems unnecessary. There are however many ways to solve the problem and others might have some better suggestions as well. Hope this helps,

  • Options
    DHawthorneDHawthorne Posts: 4,584
    I guess I'm having trouble with the question too - why is the ATOI function a problem? Is is because the projector string is not ASCII and you are trying to convert $01 to an integer and it's coming up 0 (ASCII for "'1'" being $31)?

    If that is the case, just break out the individual element and don't use the MID_STRING function like Reese said: cPROJECTOR_MESSAGE[nPROJECTOR][2] instead of ATOI(MID_STRING(cPROJECTOR_MESSAGE[GET_LAST(dPROJECTORS)],2,1), and you won't get the conversion error. You may get a "trying to assign CHAR to INTEGER" message, but you can use TYPE_CAST to suppress that, it's harmless in this case.
Sign In or Register to comment.