Home AMX User Forum AMXForums Archive Threads AMX Applications and Solutions

ATOI error

I don't understand why doesn't this work...
cOutput[cCount_] = ATOI(cString[cCount_])
when this does...
cOutput[cCount_] = cString[cCount_] - '0'

Here's the code...
DEFINE_FUNCTION ParseVideo (CHAR cString[])
STACK_VAR CHAR cCount_ CHAR cOutput[7]
{
  SEND_STRING 0, "'ParseVideo cString: ', cString"
  FOR (cCount_ = 1; cCount_ < 7; cCount_++)
  {
    cOutput[cCount_] = ATOI(cString[cCount_])  // this produces CIpLibrary::Atoi - Error
    //cOutput[cCount_] = cString[cCount_] - '0'   // this works
    SEND_STRING 0, "'ParseVideo cString[', ITOA(cCount_), ']: ', cString[cCount_], ' cOutput[', ITOA(cCount_) ,']: ', ITOA(cOutput[cCount_])"
  }
  cString = RIGHT_STRING(cString,LENGTH_STRING(cString)-6)				
  SEND_STRING 0, "'ParseVideo cString: ', cString"
}
Using the line...
cOutput[cCount_] = ATOI(cString[cCount_])
...this output is produced:

Line 1 :: ParseVideo cString: 123445 - 11:39:18
Line 2 :: CIpLibrary::Atoi - Error - 11:39:18
Line 3 :: Library Call error on Line 746 - 11:39:18
Line 4 :: ParseVideo cString[1]: 1 cOutput[1]: 0 - 11:39:18
Line 5 :: CIpLibrary::Atoi - Error - 11:39:18
...and so on

Using the line...
cOutput[cCount_] = cString[cCount_] - '0'
...this output is produced:
Line 1 :: ParseVideo cString: 123445 - 11:55:18
Line 2 :: ParseVideo cString[1]: 1 cOutput[1]: 1 - 11:55:18
[/code]


champ

Comments

  • GSLogicGSLogic Posts: 562
    Champ

    "cCount_" and "cOutput" are CHAR string, so you can't use ATOI() if you are sending to a CHAR string. if cOutput was an INTEGER then it would work.
  • viningvining Posts: 4,368
    First you're trying to use a CHAR Variable to hold integer values of your "FOR Loop" (cCount_), then you try to make the CHAR Variable cOutput(cCount_] = the CHAR cString[cCount_] variable which would be fine if you didn't covert that to an integer value by the ATOI.

    I don't know what your trying to do but this may help!
    DEFINE_CONSTANT
    crlf[2] = {$0D,$0A}
    
    DEFINE_FUNCTION ParseVideo (CHAR cString[])
    STACK_VAR CHAR  cOutput[7]
    STACK_VAR Integer nCount_
    {
      SEND_STRING 0, "'Parse Video String: ',cString,crlf"
      FOR (nCount_ = 1; nCount_ < 7; nCount_++)
      {
        cOutput[nCount_] = cString[nCount_]
        SEND_STRING 0, "'Parse Video String: ',cString[nCount_],crlf"
        SEND_STRING 0, "'Parse Video Output: ',cOutput[nCount_],crlf" 
      }
      cString = RIGHT_STRING(cString,LENGTH_STRING(cString)-6)				
      SEND_STRING 0, "'ParseVideo cString: ',cString,crlf"
    }
    
    
  • champchamp Posts: 261
    Changing cOuntput and cCount_ to an integer didn't solve the problem.

    It was sloppy programming not using an INTEGER or TYPE_CAST for cOutput but according to the Netlinx manual type conversion rules the program should just truncate the two higher bytes leaving me with a CHAR value that still works and a compiler warning which didn't happen.

    In my program cOutput[] is a global which holds the value of what input is routed to each output of a video matrix. I just changed it to a local for testing.

    I want to convert the value from ASCII to a char (or integer) because I use the code with other switchers with up to 255 inputs.
  • Joe HebertJoe Hebert Posts: 2,159
    champ wrote:
    I don't understand why doesn't this work...
    cOutput[cCount_] = ATOI(cString[cCount_])
    when this does...
    cOutput[cCount_] = cString[cCount_] - '0'
    Champ,

    What you have should work just fine with one minor adjustment.

    Change:
    cOutput[cCount_] = ATOI(cString[cCount_])

    To:
    cOutput[cCount_] = ATOI(?cString[cCount_]?)

    See this thread for an explanation:

    http://www.amxforums.com/showthread.php?t=1050
  • DHawthorneDHawthorne Posts: 4,584
    Several NetLinx functions are finicky that way, you need to enclose the variable in quotes for it to be seen properly as a string. NetLinx does not see a single CHAR variable as a string of length 1 unless you enclose it in double quotes. FIND_STRING, REMOVE_STRING, ATOI are all examples of this. If you give them a variable that is a single CHAR in length, you have to put it in double quotes for it to work right.
  • champchamp Posts: 261
    That was it!
    The working line is...

    cOutput[cCount_] = TYPE_CAST(ATOI("cString[cCount_]")


    Thanks
    Champ
  • Joe HebertJoe Hebert Posts: 2,159
    It doesn?t hurt any but there is no need for the TYPE_CAST in this situation. Your code doesn?t generate any warnings and there is no way to stuff a number bigger than 255 into cString[cCount_] . Your code should work the same with or without the TYPE_CAST, at least that?s my understanding of the language.
Sign In or Register to comment.