Check numeric characters

Dear All,
I have an amplifier which replies the same string sent to as well as the expected reply. For example:

Sending power on command:
'Z1POW1;'

the reply:
'Z1POW1;Z1POW1;' // the first one up to semicolon is the command as it has been sent while the second the reply

I need to know the power status so I'm using a data_event on "string" section.

But when I need to request the power status, I should send the 'Z1POW?;' command and the reply is:
'Z1POW?;Z1POW1;'

My question is how I can check that the character after the 'Z1POW' is a numeric or a character ? In my case, I don't care about the "?" but the "1".

data_event[dvAMP]
{
string:
{
local_var char cString[50]
local_var char oneline_grap[20]

cString = data.text

while (find_string(cString,';',1) )
{
oneline_grap= remove_string(cString,';',1)

     if (find_string(oneline_grap,'Z1POW',1))  //Z1POW?;Z1POW1;
     {
        remove_string(oneline_grap,'POW',1)
        if ( atoi(oneline_grap) ) // is it a number ?
             {
                nPower = atoi(oneline_grap)
                send_string 0,"'Power = ', itoa(nPower)"
            }
    }

}

The above does not work reliable as the atoi(oneline_grap) is zero both for the number '0' as well as for any other non-numeric character.
I know that I can override this with some more piece of code but I also really like to know if there is a way to recognize between zero number and non- numeric character.

George

Comments

  • HARMAN_icraigieHARMAN_icraigie Technical Trainer II, Harman Professional University Posts: 483

    evaluate the penultimate character in the string

    if(find_string(oneline_grap,'Z1POW',1)
    {
      if(online_grap[length_string(online_grap) - 1] >= '0' && online_grap[length_string(online_grap) - 1] <= '9' )
      {
         nPower = atoi(oneline_grap)
         send_string 0,"'Power = ', itoa(nPower)"
      }
    }
    

    OR if you prefer hex values

    if(find_string(oneline_grap,'Z1POW',1)
    {
      if(online_grap[length_string(online_grap) - 1] >= $30 && online_grap[length_string(online_grap) - 1] <= $39 )
      {
         nPower = atoi(oneline_grap)
         send_string 0,"'Power = ', itoa(nPower)"
      }
    }
    
  • richardhermanrichardherman not-so-junior member Posts: 220

    @George Krietsepis said:

    if there is a way to recognize between zero number and non- numeric character.

    As you noticed the ATOI function has the somewhat odd behaviour that both ASCII zero ( '0', $30) and any non-numeric character both give you the result 0. It would have been much easier if any non-numeric character would result in -1. So if zero (0) is a value you are interested in, be careful with ATOI. I usually do something like Ian described, just evaluate the ASCII itself.

Sign In or Register to comment.